栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

vue组件之间通信方式实例总结

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue组件之间通信方式实例总结

本文实例总结了vue组件之间通信方式。分享给大家供大家参考,具体如下:

对于vue来说,组件之间的消息传递是非常重要的,下面是我对组件之间消息传递的各种方式的总结,总共有8种方式。

1. props和$emit

父组件向子组件传递数据是通过prop传递的,子组件传递数据给父组件是通过$emit触发事件来做到的。

Vue.component('child',{
    data(){
      return {
 mymessage:this.message
      }
    },
    template:`
      
  
    `,
    props:['message'],//得到父组件传递过来的数据
    methods:{
      passData(val){
 //触发父组件中的事件
 this.$emit('getChildData',val)
      }
    }
  })
  Vue.component('parent',{
    template:`
      
 

this is parent compoent!

`, data(){ return { message:'hello' } }, methods:{ //执行子组件触发的事件 getChildData(val){ console.log(val) } } }) var app=new Vue({ el:'#app', template:` ` })

在上面的例子中,有父组件parent和子组件child。

1).父组件传递了message数据给子组件,并且通过v-on绑定了一个getChildData事件来监听子组件的触发事件;

2).子组件通过props得到相关的message数据,最后通过this.$emit触发了getChildData事件。

2.$attrs和$listeners

第一种方式处理父子组件之间的数据传输有一个问题:如果父组件A下面有子组件B,组件B下面有组件C,这时如果组件A想传递数据给组件C怎么办呢?

如果采用第一种方法,我们必须让组件A通过prop传递消息给组件B,组件B在通过prop传递消息给组件C;要是组件A和组件C之间有更多的组件,那采用这种方式就很复杂了。Vue 2.4开始提供了$attrs和$listeners来解决这个问题,能够让组件A之间传递消息给组件C。

Vue.component('C',{
    template:`
      
  
    `,
    methods:{
      passCData(val){
 //触发父组件A中的事件
 this.$emit('getCData',val)
      }
    }
  })
  Vue.component('B',{
    data(){
      return {
 mymessage:this.message
      }
    },
    template:`
      
 
 
 
 
      
    `,
    props:['message'],//得到父组件传递过来的数据
    methods:{
      passData(val){
 //触发父组件中的事件
 this.$emit('getChildData',val)
      }
    }
  })
  Vue.component('A',{
    template:`
      
 

this is parent compoent!

`, data(){ return { message:'hello', messagec:'hello c' //传递给c组件的数据 } }, methods:{ getChildData(val){ console.log('这是来自B组件的数据') }, //执行C子组件触发的事件 getCData(val){ console.log("这是来自C组件的数据:"+val) } } }) var app=new Vue({ el:'#app', template:` ` })

3. 中央事件总线

上面两种方式处理的都是父子组件之间的数据传递,而如果两个组件不是父子关系呢?这种情况下可以使用中央事件总线的方式。新建一个Vue事件bus对象,然后通过bus.$emit触发事件,bus.$on监听触发的事件。

Vue.component('brother1',{
    data(){
      return {
 mymessage:'hello brother1'
      }
    },
    template:`
      
 

this is brother1 compoent!

`, methods:{ passData(val){ //触发全局事件globalEvent bus.$emit('globalEvent',val) } } }) Vue.component('brother2',{ template:`

this is brother2 compoent!

brother1传递过来的数据:{{brothermessage}}

`, data(){ return { mymessage:'hello brother2', brothermessage:'' } }, mounted(){ //绑定全局事件globalEvent bus.$on('globalEvent',(val)=>{ this.brothermessage=val; }) } }) //中央事件总线 var bus=new Vue(); var app=new Vue({ el:'#app', template:` ` })

4. provide和inject

父组件中通过provider来提供变量,然后在子组件中通过inject来注入变量。不论子组件有多深,只要调用了inject那么就可以注入provider中的数据。而不是局限于只能从当前父组件的prop属性来获取数据,只要在父组件的生命周期内,子组件都可以调用。

Vue.component('child',{
    inject:['for'],//得到父组件传递过来的数据
    data(){
      return {
 mymessage:this.for
      }
    },
    template:`
      
 
      
  })
  Vue.component('parent',{
    template:`
      
 

this is parent compoent!

`, provide:{ for:'test' }, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` ` })

5. v-model

父组件通过v-model传递值给子组件时,会自动传递一个value的prop属性,在子组件中通过this.$emit(‘input',val)自动修改v-model绑定的值

Vue.component('child',{
    props:{
      value:String, //v-model会自动传递一个字段为value的prop属性
    },
    data(){
      return {
 mymessage:this.value
      }
    },
    methods:{
      changevalue(){
 this.$emit('input',this.mymessage);//通过如此调用可以改变父组件上v-model绑定的值
      }
    },
    template:`
      
 
      
  })
  Vue.component('parent',{
    template:`
      
 

this is parent compoent!

{{message}}

`, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` ` })

6. $parent和$children

Vue.component('child',{
    props:{
      value:String, //v-model会自动传递一个字段为value的prop属性
    },
    data(){
      return {
 mymessage:this.value
      }
    },
    methods:{
      changevalue(){
 this.$parent.message = this.mymessage;//通过如此调用可以改变父组件的值
      }
    },
    template:`
      
 
      
  })
  Vue.component('parent',{
    template:`
      
 

this is parent compoent!

`, methods:{ changeChildValue(){ this.$children[0].mymessage = 'hello'; } }, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` ` })

7. boradcast和dispatch

vue1.0中提供了这种方式,但vue2.0中没有,但很多开源软件都自己封装了这种方式,比如min ui、element ui和iview等。

比如如下代码,一般都作为一个mixins去使用, broadcast是向特定的父组件,触发事件,dispatch是向特定的子组件触发事件,本质上这种方式还是on和on和emit的封装,但在一些基础组件中却很实用。

function broadcast(componentName, eventName, params) {
 this.$children.forEach(child => {
  var name = child.$options.componentName;
  if (name === componentName) {
   child.$emit.apply(child, [eventName].concat(params));
  } else {
   broadcast.apply(child, [componentName, eventName].concat(params));
  }
 });
}
export default {
 methods: {
  dispatch(componentName, eventName, params) {
   var parent = this.$parent;
   var name = parent.$options.componentName;
   while (parent && (!name || name !== componentName)) {
    parent = parent.$parent;
    if (parent) {
     name = parent.$options.componentName;
    }
   }
   if (parent) {
    parent.$emit.apply(parent, [eventName].concat(params));
   }
  },
  broadcast(componentName, eventName, params) {
   broadcast.call(this, componentName, eventName, params);
  }
 }
};

8. vuex处理组件之间的数据交互

如果业务逻辑复杂,很多组件之间需要同时处理一些公共的数据,这个时候才有上面这一些方法可能不利于项目的维护,vuex的做法就是将这一些公共的数据抽离出来,然后其他组件就可以对这个公共数据进行读写操作,这样达到了解耦的目的。

详情可参考:https://vuex.vuejs.org/zh-cn/

希望本文所述对大家vue.js程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/77058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号