Vue组件间怎么通信?本篇文章盘点Vue2和Vue3的10种组件通信方式,希望对大家有所帮助!
Vue中组件通信方式有很多,其中Vue2和Vue3实现起来也会有很多差异;本文将通过选项式API
组合式API
以及setup
三种不同实现方式全面介绍Vue2和Vue3的组件通信方式。其中将要实现的通信方式如下表所示。(学习视频分享:vue视频教程)
方式 | Vue2 | Vue3 |
---|---|---|
父传子 | props | props |
子传父 | $emit | emits |
父传子 | $attrs | attrs |
子传父 | $listeners | 无(合并到 attrs方式) |
父传子 | provide | provide |
子传父 | inject | inject |
子组件访问父组件 | $parent | 无 |
父组件访问子组件 | $children | 无 |
父组件访问子组件 | $ref | expose&ref |
兄弟传值 | EventBus | mitt |
props
props是组件通信中最常用的通信方式之一。父组件通过v-bind传入,子组件通过props接收,下面是它的三种实现方式
- 选项式API
//父组件 <template> <div> <Child :msg="parentMsg" /> </div> </template> <script> import Child from './Child' export default { components:{ Child }, data() { return { parentMsg: '父组件信息' } } } </script> //子组件 <template> <div> {{msg}} </div> </template> <script> export default { props:['msg'] } </script>
- 组合式Api
//父组件 <template> <div> <Child :msg="parentMsg" /> </div> </template> <script> import { ref,defineComponent } from 'vue' import Child from './Child.vue' export default defineComponent({ components:{ Child }, setup() { const parentMsg = ref('父组件信息') return { parentMsg }; }, }); </script> //子组件 <template> <div> {{ parentMsg }} </div> </template> <script> import { defineComponent,toRef } from "vue"; export default defineComponent({ props: ["msg"],// 如果这行不写,下面就接收不到 setup(props) { console.log(props.msg) //父组件信息 let parentMsg = toRef(props, 'msg') return { parentMsg }; }, }); </script>
- setup语法糖
//父组件 <template> <div> <Child :msg="parentMsg" /> </div> </template> <script setup> import { ref } from 'vue' import Child from './Child.vue' const parentMsg = ref('父组件信息') </script> //子组件 <template> <div> {{ parentMsg }} </div> </template> <script setup> import { toRef, defineProps } from "vue"; const props = defineProps(["msg"]); console.log(props.msg) //父组件信息 let parentMsg = toRef(props, 'msg') </script>
注意
props中数据流是单项的,即子组件不可改变父组件传来的值
在组合式API中,如果想在子组件中用其它变量接收props的值时需要使用toRef将props中的属性转为响应式。
emit
子组件可以通过emit发布一个事件并传递一些参数,父组件通过v-on进行这个事件的监听
- 选项式API
//父组件 <template> <div> <Child @sendMsg="getFromChild" /> </div> </template> <script> import Child from './Child' export default { components:{ Child }, methods: { getFromChild(val) { console.log(val) //我是子组件数据 } } } </script> // 子组件 <template> <div> <button @click="sendFun">send</button> </div> </template> <script> export default { methods:{ sendFun(){ this.$emit('sendMsg','我是子组件数据') } } } </script>
- 组合式Api
//父组件 <template> <div> <Child @sendMsg="getFromChild" /> </div> </template> <script> import Child from './Child' import { defineComponent } from "vue"; export default defineComponent({ components: { Child }, setup() { const getFromChild = (val) => { console.log(val) //我是子组件数据 } return { getFromChild }; }, }); </script> //子组件 <template> <div> <button @click="sendFun">send</button> </div> </template> <script> import { defineComponent } from "vue"; export default defineComponent({ emits: ['sendMsg'], setup(props, ctx) { const sendFun = () => { ctx.emit('sendMsg', '我是子组件数据') } return { sendFun }; }, }); </script>
- setup语法糖
//父组件 <template> <div> <Child @sendMsg="getFromChild" /> </div> </template> <script setup> import Child from './Child' const getFromChild = (val) => { console.log(val) //我是子组件数据 } </script> //子组件 <template> <div> <button @click="sendFun">send</button> </div> </template> <script setup> import { defineEmits } from "vue"; const emits = defineEmits(['sendMsg']) const sendFun = () => { emits('sendMsg', '我是子组件数据') } </script>
attrs和listeners
子组件使用$attrs可以获得父组件除了props传递的属性和特性绑定属性 (class和 style)之外的所有属性。
子组件使用$listeners可以获得父组件(不含.native修饰器的)所有v-on事件监听器,在Vue3中已经不再使用;但是Vue3中的attrs不仅可以获得父组件传来的属性也可以获得父组件v-on事件监听器
- 选项式API
//父组件 <template> <div> <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2" /> </div> </template> <script> import Child from './Child' export default { components:{ Child }, data(){ return { msg1:'子组件msg1', msg2:'子组件msg2' } }, methods: { parentFun(val) { console.log(`父组件方法被调用,获得子组件传值:${val}`) } } } </script> //子组件 <template> <div> <button @click="getParentFun">调用父组件方法</button> </div> </template> <script> export default { methods:{ getParentFun(){ this.$listeners.parentFun('我是子组件数据') } }, created(){ //获取父组件中所有绑定属性 console.log(this.$attrs) //{"msg1": "子组件msg1","msg2": "子组件msg2"} //获取父组件中所有绑定方法 console.log(this.$listeners) //{parentFun:f} } } </script>
- 组合式API
//父组件 <template> <div> <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2" /> </div> </template> <script> import Child from './Child' import { defineComponent,ref } from "vue"; export default defineComponent({ components: { Child }, setup() { const msg1 = ref('子组件msg1') const msg2 = ref('子组件msg2') const parentFun = (val) => { console.log(`父组件方法被调用,获得子组件传值:${val}`) } return { parentFun, msg1, msg2 }; }, }); </script> //子组件 <template> <div> <button @click="getParentFun">调用父组件方法</button> </div> </template> <script> import { defineComponent } from "vue"; export default defineComponent({ emits: ['sendMsg'], setup(props, ctx) { //获取父组件方法和事件 console.log(ctx.attrs) //Proxy {"msg1": "子组件msg1","msg2": "子组件msg2"} const getParentFun = () => { //调用父组件方法 ctx.attrs.onParentFun('我是子组件数据') } return { getParentFun }; }, }); </script>
- setup语法糖
//父组件 <template> <div> <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2" /> </div> </template> <script setup> import Child from './Child' import { ref } from "vue"; const msg1 = ref('子组件msg1') const msg2 = ref('子组件msg2') const parentFun = (val) => { console.log(`父组件方法被调用,获得子组件传值:${val}`) } </script> //子组件 <template> <div> <button @click="getParentFun">调用父组件方法</button> </div> </template> <script setup> import { useAttrs } from "vue"; const attrs = useAttrs() //获取父组件方法和事件 console.log(attrs) //Proxy {"msg1": "子组件msg1","msg2": "子组件msg2"} const getParentFun = () => { //调用父组件方法 attrs.onParentFun('我是子组件数据') } </script>
注意
Vue3中使用attrs调用父组件方法时,方法前需要加上on;如parentFun->onParentFun
provide/inject
provide:是一个对象,或者是一个返回对象的函数。里面包含要给子孙后代属性
inject:一个字符串数组,或者是一个对象。获取父组件或更高层次的组件provide的值,既在任何后代组件都可以通过inject获得
- 选项式API
//父组件 <script> import Child from './Child' export default { components: { Child }, data() { return { msg1: '子组件msg1', msg2: '子组件msg2' } }, provide() { return { msg1: this.msg1, msg2: this.msg2 } } } </script> //子组件 <script> export default { inject:['msg1','msg2'], created(){ //获取高层级提供的属性 console.log(this.msg1) //子组件msg1 console.log(this.msg2) //子组件msg2 } } </script>
- 组合式API
//父组件 <script> import Child from './Child' import { ref, defineComponent,provide } from "vue"; export default defineComponent({ components:{ Child }, setup() { const msg1 = ref('子组件msg1') const msg2 = ref('子组件msg2') provide("msg1", msg1) provide("msg2", msg2) return { } }, }); </script> //子组件 <template> <div> <button @click="getParentFun">调用父组件方法</button> </div> </template> <script> import { inject, defineComponent } from "vue"; export default defineComponent({ setup() { console.log(inject('msg1').value) //子组件msg1 console.log(inject('msg2').value) //子组件msg2 }, }); </script>
- setup语法糖
//父组件 <script setup> import Child from './Child' import { ref,provide } from "vue"; const msg1 = ref('子组件msg1') const msg2 = ref('子组件msg2') provide("msg1",msg1) provide("msg2",msg2) </script> //子组件 <script setup> import { inject } from "vue"; console.log(inject('msg1').value) //子组件msg1 console.log(inject('msg2').value) //子组件msg2 </script>
说明
provide/inject一般在深层组件嵌套中使用合适。一般在组件开发中用的居多。
parent/children
$parent: 子组件获取父组件Vue实例,可以获取父组件的属性方法等
$children: 父组件获取子组件Vue实例,是一个数组,是直接儿子的集合,但并不保证子组件的顺序
- Vue2
import Child from './Child' export default { components: { Child }, created(){ console.log(this.$children) //[Child实例] console.log(this.$parent)//父组件实例 } }
注意父组件获取到的$children
并不是响应式的
expose&ref
$refs可以直接获取元素属性,同时也可以直接获取子组件实例
- 选项式API
//父组件 <template> <div> <Child ref="child" /> </div> </template> <script> import Child from './Child' export default { components: { Child }, mounted(){ //获取子组件属性 console.log(this.$refs.child.msg) //子组件元素 //调用子组件方法 this.$refs.child.childFun('父组件信息') } } </script> //子组件 <template> <div> <div></div> </div> </template> <script> export default { data(){ return { msg:'子组件元素' } }, methods:{ childFun(val){ console.log(`子组件方法被调用,值${val}`) } } } </script>
- 组合式API
//父组件 <template> <div> <Child ref="child" /> </div> </template> <script> import Child from './Child' import { ref, defineComponent, onMounted } from "vue"; export default defineComponent({ components: { Child }, setup() { const child = ref() //注意命名需要和template中ref对应 onMounted(() => { //获取子组件属性 console.log(child.value.msg) //子组件元素 //调用子组件方法 child.value.childFun('父组件信息') }) return { child //必须return出去 否则获取不到实例 } }, }); </script> //子组件 <template> <div> </div> </template> <script> import { defineComponent, ref } from "vue"; export default defineComponent({ setup() { const msg = ref('子组件元素') const childFun = (val) => { console.log(`子组件方法被调用,值${val}`) } return { msg, childFun } }, }); </script>
- setup语法糖
//父组件 <template> <div> <Child ref="child" /> </div> </template> <script setup> import Child from './Child' import { ref, onMounted } from "vue"; const child = ref() //注意命名需要和template中ref对应 onMounted(() => { //获取子组件属性 console.log(child.value.msg) //子组件元素 //调用子组件方法 child.value.childFun('父组件信息') }) </script> //子组件 <template> <div> </div> </template> <script setup> import { ref,defineExpose } from "vue"; const msg = ref('子组件元素') const childFun = (val) => { console.log(`子组件方法被调用,值${val}`) } //必须暴露出去父组件才会获取到 defineExpose({ childFun, msg }) </script>
注意
通过ref获取子组件实例必须在页面挂载完成后才能获取。
在使用setup语法糖时候,子组件必须元素或方法暴露出去父组件才能获取到
EventBus/mitt
兄弟组件通信可以通过一个事件中心EventBus实现,既新建一个Vue实例来进行事件的监听,触发和销毁。
在Vue3中没有了EventBus兄弟组件通信,但是现在有了一个替代的方案mitt.js
,原理还是 EventBus
- 选项式API
//组件1 <template> <div> <button @click="sendMsg">传值</button> </div> </template> <script> import Bus from './bus.js' export default { data(){ return { msg:'子组件元素' } }, methods:{ sendMsg(){ Bus.$emit('sendMsg','兄弟的值') } } } </script> //组件2 <template> <div> 组件2 </div> </template> <script> import Bus from './bus.js' export default { created(){ Bus.$on('sendMsg',(val)=>{ console.log(val);//兄弟的值 }) } } </script> //bus.js import Vue from "vue" export default new Vue()
- 组合式API
首先安装mitt
npm i mitt -S
然后像Vue2中bus.js
一样新建mitt.js
文件
mitt.js
import mitt from 'mitt' const Mitt = mitt() export default Mitt
//组件1 <template> <button @click="sendMsg">传值</button> </template> <script> import { defineComponent } from "vue"; import Mitt from './mitt.js' export default defineComponent({ setup() { const sendMsg = () => { Mitt.emit('sendMsg','兄弟的值') } return { sendMsg } }, }); </script> //组件2 <template> <div> 组件2 </div> </template> <script> import { defineComponent, onUnmounted } from "vue"; import Mitt from './mitt.js' export default defineComponent({ setup() { const getMsg = (val) => { console.log(val);//兄弟的值 } Mitt.on('sendMsg', getMsg) onUnmounted(() => { //组件销毁 移除监听 Mitt.off('sendMsg', getMsg) }) }, }); </script>
- setup语法糖
//组件1 <template> <button @click="sendMsg">传值</button> </template> <script setup> import Mitt from './mitt.js' const sendMsg = () => { Mitt.emit('sendMsg', '兄弟的值') } </script> //组件2 <template> <div> 组件2 </div> </template> <script setup> import { onUnmounted } from "vue"; import Mitt from './mitt.js' const getMsg = (val) => { console.log(val);//兄弟的值 } Mitt.on('sendMsg', getMsg) onUnmounted(() => { //组件销毁 移除监听 Mitt.off('sendMsg', getMsg) }) </script>
写在最后
其实组件还可以借助Vuex或者Pinia状态管理工具进行通信(但是组件之间的通信一般不建议这样做,因为这样就会出现组件不能复用的问题)。对于Vuex和Pinia的用法大家可以参考这篇文章一文解析Pinia和Vuex
以上是组件间怎么通信?盘点Vue组件通信方式(值得收藏)的详细内容。更多信息请关注PHP中文网其他相关文章!

Netflix在前端技术上的选择主要集中在性能优化、可扩展性和用户体验三个方面。1.性能优化:Netflix选择React作为主要框架,并开发了SpeedCurve和Boomerang等工具来监控和优化用户体验。2.可扩展性:他们采用微前端架构,将应用拆分为独立模块,提高开发效率和系统扩展性。3.用户体验:Netflix使用Material-UI组件库,通过A/B测试和用户反馈不断优化界面,确保一致性和美观性。

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVueDirectly.1)TeamExperience:selectBasedAsedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects,vueforsimplerprojects,reactforforforecomplexones.3)cocatizationNeedsneeds:reactofficatizationneedneeds:reactofferizationneedneedneedneeds:reactoffersizatization needeffersefersmoreflexiblesimore.4)ecosyaka

Netflix在框架选择上主要考虑性能、可扩展性、开发效率、生态系统、技术债务和维护成本。1.性能与可扩展性:选择Java和SpringBoot以高效处理海量数据和高并发请求。2.开发效率与生态系统:使用React提升前端开发效率,利用其丰富的生态系统。3.技术债务与维护成本:选择Node.js构建微服务,降低维护成本和技术债务。

Netflix主要使用React作为前端框架,辅以Vue用于特定功能。1)React的组件化和虚拟DOM提升了Netflix应用的性能和开发效率。2)Vue在Netflix的内部工具和小型项目中应用,其灵活性和易用性是关键。

Vue.js是一种渐进式JavaScript框架,适用于构建复杂的用户界面。1)其核心概念包括响应式数据、组件化和虚拟DOM。2)实际应用中,可以通过构建Todo应用和集成VueRouter来展示其功能。3)调试时,建议使用VueDevtools和console.log。4)性能优化可通过v-if/v-show、列表渲染优化和异步加载组件等实现。

Vue.js适合小型到中型项目,而React更适用于大型、复杂应用。1.Vue.js的响应式系统通过依赖追踪自动更新DOM,易于管理数据变化。2.React采用单向数据流,数据从父组件流向子组件,提供明确的数据流向和易于调试的结构。

Vue.js适合中小型项目和快速迭代,React适用于大型复杂应用。1)Vue.js易于上手,适用于团队经验不足或项目规模较小的情况。2)React的生态系统更丰富,适合有高性能需求和复杂功能需求的项目。

实现 Vue 中 a 标签跳转的方法包括:HTML 模板中使用 a 标签指定 href 属性。使用 Vue 路由的 router-link 组件。使用 JavaScript 的 this.$router.push() 方法。可通过 query 参数传递参数,并在 router 选项中配置路由以进行动态跳转。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

WebStorm Mac版
好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1
功能强大的PHP集成开发环境