Vue组件间通信(六种方式)

第一种:props

适用场景:父子组件通信

注意事项:

如果父组件给子组件传递数据(函数):本质上上是子组件给父组件传递数据

如果父组件给子组件传递的数据(非函数),本质上是父组件给子组件传递数据

书写方式: 3种

['todos]、{type:Array}、{type:Array, default:[]}

第二种:自定义事件

适用场景:子组件给父组件传递数据

$on、$emit  $off

子组件传递值:

this.$emit('personalEvent', this.name);

父组件接受值:

mounted() {

this.$refs.student.$on('personalEvent', (name) => {

       console.log(this);

       console.log(name);

       this.studentName = name;

    });

}

第三种:全局事件总线$bus

适用场景:万能

Vue.prototype.$bus = this;

new Vue({

    el:'#app',

    render: h => h(App),

    beforeCreate() {

        Vue.prototype.$bus = this; //安装全局事件总线

    }

});

子组件:

this.$bus.$emit('hello', this.name);

父组件:

mounted() {

    console.log('School', this);

    this.$bus.$on('hello', (data) => {

      console.log(`我是School组件,我收到了数据,${data}`);

    })

  },

  beforeDestroy() {

    this.$bus.$off('hello'); //销毁解绑

  }

第四种:消息订阅和发布 pubsub-js,在React框架中使用的比较多(发布、订阅)

适用场景:万能

发布消息:

pubsub.publish('hello', this.name);

订阅消息:

mounted() {

    // console.log('School', this);

    // this.$bus.$on('hello', (data) => {

    //   console.log(`我是School组件,我收到了数据,${data}`);

    // })

    //订阅消息 隔空对讲机喊话

    this.pubId = pubsub.subscribe('hello',  (name, msg) => {

      //注意这里写剪头函数this才不会丢

      console.log(`有人发布了hello消息,回调被执行,data: ${msg}`);

    });

  },

  beforeDestroy() {

    // this.$bus.$off('hello'); //销毁解绑

    //取消订阅

    pubsub.unsubscribe(this.pubId); //取消订阅

  }

第五种:VueX多组件数据共享

适用场景:万能

5.1main.js文件配置:

new Vue({

    el: '#app',

    store,

    render: h => h(App),

});

5.2创建并暴露store

export default new Vuex.Store({

    actions,

    mutations,

    state,

    getters

});

5.3 引入与使用

import { mapState, mapGetters, mapMutations, mapActions} from 'vuex';

computed:{

... mapState(['sum', 'school', 'subject', 'personList']),

...mapGetters(['bigSum']),

}

methods:{

  ...mapActions(['incrementWait', 'incrementIfOdd']), //数组写法,同上

  ...mapMutations({

      increment: 'INCREMENT',

      decrement: 'DECREMENT',

    }), //对象写法

...mapMutations(['increment', 'decrement']),//数组写法

     incrementOdd(){

       this.$store.dispatch('incrementIfOdd', this.n);

     },

}

第六种:插槽

适用场景:父子组件通信--(一般传递的是结构)

默认插槽

具名插槽:

  <slot name="center">我是默认值</slot>

  <slot name="footer">我是默认值</slot>

<Category title="美食">

       <img slot="center" src="" alt="delicious food"/>

       <a href="https://www.baidu.com" slot="footer">百度</a>

     </Category>

作用域插槽:

   <slot :games="games">

      我是默认值

    </slot>

   <Category title="游戏">

       <template slot-scope="{games}">

         <h4 v-for="(g , index) in games" :key="index">{{ g }}</h4>

       </template>

     </Category>

  • 21
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,它允许您轻松地构建可重用的组件Vue组件是一个独立的功能部件,它可以通过不同的方式进行通信,以实现更好的交互和协作。以下是Vue六种常用的组件间通信方式: 1. Props Props是一种用于从父组件向子组件传递数据的机制。父组件可以将数据作为属性传递给子组件,并且子组件可以通过props属性来使用这些数据。 2. Events Events是一种Vue组件间通信的双向机制,它允许子组件通过触发事件来向父组件发送消息。父组件可以监听这些事件,并根据需要作出响应。 3. Provide / Inject Provide / Inject是一种用于跨深度嵌套组件之间共享数据的机制。父组件可以通过provide属性将数据传递给子组件,然后子组件可以通过inject属性来访问这些数据。 4. Vuex Vuex是一种Vue状态管理器,它允许您在单个应用程序管理共享状态。Vuex包含一个央状态存储库,可以跨组件访问和修改状态,以实现更好的协作和交互。 5. ref 属性 Ref是一种用于访问组件实例的方法,它可以通过设置ref属性来指定组件的名称。然后你就可以通过$refs属性来访问该组件实例,并在组件之间进行通信。 6. $emit 和 $on $emit和$on是Vue的内置事件机制,它允许组件之间进行通信。$emit用于触发事件,而$on则用于监听事件。使用这种方式,您可以轻松地在组件之间传递消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值