当前位置: 首页 > news >正文

Vue组件之间的通信

一、通信方式

  • Props 和 Events:通过父组件传递 props 给子组件,子组件使用 $emit 发送事件到父组件。
  • Event Bus:使用一个中央事件总线来跨组件通信。
  • Vuex:使用 Vuex 进行全局状态管理,以便在任何组件间共享状态。
  • Provide / Inject:祖先组件使用 provide 传递数据,后代组件通过 inject 接收数据。
  • $refs:父组件通过 ref 获取子组件实例,直接调用其方法或访问属性。
  • Scoped Slots:使用插槽在父组件中传递数据到子组件,允许子组件根据传递的数据进行渲染。

二、应用举例 

1、Props 和 Events: 

        父组件 (Parent.vue

<template><Child :message="parentMessage" @update="handleUpdate" />
</template>
<script>
export default {data() {return { parentMessage: 'Hello from parent' };},methods: {handleUpdate(newMessage) {this.parentMessage = newMessage;}}
}
</script>

        子组件 (Child.vue

<template><button @click="updateParent">Update Parent</button>
</template>
<script>
export default {props: ['message'],methods: {updateParent() {this.$emit('update', 'Hello from child');}}
}
</script>

2、Event Bus: 

        事件总线 (eventBus.js

import Vue from 'vue';
export const EventBus = new Vue();

        发送消息 (ComponentA.vue

<template><button @click="sendMessage">Send Message</button>
</template>
<script>
import { EventBus } from './eventBus';
export default {methods: {sendMessage() {EventBus.$emit('message', 'Hello from Component A');}}
}
</script>

        接收消息 (ComponentB.vue

<template><p>{{ receivedMessage }}</p>
</template>
<script>
import { EventBus } from './eventBus';
export default {data() {return { receivedMessage: '' };},created() {EventBus.$on('message', (message) => {this.receivedMessage = message;});}
}
</script>

3、Vuex: 

         Vuex Store (store.js)

import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);export default new Vuex.Store({state: {message: 'Hello from Vuex'},mutations: {setMessage(state, newMessage) {state.message = newMessage;}}
});

        更新消息 (ComponentA.vue

<template><button @click="updateMessage">Update Message</button>
</template>
<script>
import { mapMutations } from 'vuex';
export default {methods: {...mapMutations(['setMessage']),updateMessage() {this.setMessage('Hello from Component A');}}
}
</script>

        显示消息 (ComponentB.vue

<template><p>{{ message }}</p>
</template>
<script>
import { mapState } from 'vuex';
export default {computed: {...mapState(['message'])}
}
</script>

4、Provide / Inject: 

        提供数据 (Ancestor.vue

<template><Descendant />
</template>
<script>
export default {provide() {return {message: 'Hello from ancestor'};}
}
</script>

        注入数据 (Descendant.vue

<template><p>{{ message }}</p>
</template>
<script>
export default {inject: ['message']
}
</script>

5、$refs: 

        父组件 (Parent.vue)         

<template><Child ref="childComponent" /><button @click="callChildMethod">Call Child Method</button>
</template>
<script>
import Child from './Child.vue';
export default {components: { Child },methods: {callChildMethod() {this.$refs.childComponent.someMethod();}}
}
</script>

         子组件 (Child.vue)

<template><p>Child Component</p>
</template>
<script>
export default {methods: {someMethod() {console.log('Method in child called!');}}
}
</script>

6、Scoped Slots: 

        父组件 (Parent.vue

<template><Child><template v-slot:default="slotProps"><p>{{ slotProps.message }}</p></template></Child>
</template>
<script>
import Child from './Child.vue';
export default {components: { Child }
}
</script>

         子组件 (Child.vue)

<template><slot :message="'Hello from child'"></slot>
</template>
<script>
export default {}
</script>
http://www.lryc.cn/news/424620.html

相关文章:

  • 【AI 绘画】模型转换与快速生图(基于diffusers)
  • 甄选范文“论软件设计方法及其应”软考高级论文系统架构设计师论文
  • leetcode线段树(2940. 找到 Alice 和 Bob 可以相遇的建筑)
  • 用于不平衡医疗数据分类的主动SMOTE
  • linux文件更新日期与系统日期比较
  • leetCode - - - 哈希表
  • NGINX自动清理180天之前的日志
  • jackson 轻松搞定接口数据脱敏
  • Nginx 正则表达式与rewrite
  • tekton什么情况下在Dockerfile中需要用copy
  • 第九届世界渲染大赛在哪里提交作品呢?
  • fastjson(autoType)反序列化漏洞
  • Java入门基础16:集合框架1(Collection集合体系、List、Set)
  • Qt如何调用接口
  • Android14之解决编译libaaudio.so报错问题(二百二十七)
  • 【专题】2024年7月人工智能AI行业报告合集汇总PDF分享(附原数据表)
  • 干货分享|如何使用Stable Diffusion打造会说话的数字人?
  • OrangePi AIpro学习4 —— 昇腾AI模型推理 C++版
  • vue js 多组件异步请求解决方案
  • 【Android】不同系统版本获取设备MAC地址
  • 残差网络--NLP上的应用
  • 1章4节:数据可视化, R 语言的静态绘图和 Shiny 的交互可视化演示(更新2024/08/14)
  • 浅谈个人用户如何玩转HTTP代理
  • 动手研发实时口译系统
  • C#(asp.net)电商后台管理系统-计算机毕业设计源码70015
  • Unity 中创建动画的教程
  • 2024年最全渗透测试学习指南,小白也能轻松hold住!零基础到精通,看完这篇就够了!
  • 有道云docx转换markdown,导入hugo发布到github page,多平台发布适配
  • 如何理解:进程控制
  • 工业互联网边缘计算实训室解决方案