Vue---任意组件间消息订阅与发布(pubsub)

对比全局事件总线学习(不要混)

http://t.csdn.cn/4PYmQ

目录

总结

工作模型

下载库

引入库

一、任意组件间消息订阅与发布

  student.vue发布消息

  重要代码

  school.vue接收消息

  重要代码


总结

 

工作模型

 

下载库

引入库

        import pubsub from 'pubsub-js'

一、任意组件间消息订阅与发布

   由student.vue   向school.vue  传输数据

  student.vue发布消息


<template>
<!-- <template>标签不参与编译,在页面展现的是下面的一段结构 -->
     <!-- 组件结构 -->
     <div class="student">
        <h2 > 学生名称:{{name}}</h2>
        <h2> 学生性别:{{sex}}</h2>
        <button @click="sendStudentName">把学生名给School组件</button>
      
     </div>
</template>

 <script>
        import pubsub from 'pubsub-js'

        export default {
            name:'Student',

            data(){
                return{
                 name:'张三',
                 sex:'男',
                 age:18
                }
            },
            methods:{
              sendStudentName(){
              // 发布hello消息  带着数据
              pubsub.publish('hello',this.name)
              }
            }
        }  
</script>

<style scoped>
      .student{
        background-color: red;
        padding:5px;
        margin-top:30px;
      }
</style>




  重要代码

        import pubsub from 'pubsub-js'
            methods:{
              sendStudentName(){
              // 发布hello消息  带着数据
              pubsub.publish('hello',this.name)
              }
            }

  school.vue接收消息


<template>
     <div class="school">
        <h2> 学校名称:{{name}}</h2>
        <h2> 学校地址:{{address}}</h2>

     </div>
</template>

 <script>
        import pubsub from 'pubsub-js'

        export default {
            name:'School',
            data(){
                return{
                 name:'尚硅谷',
                 address:'北京昌平',
                }
            },
        mounted() {
            // 订阅hello消息
            // function(msgName,data) 第一个参数是msgName 消息名  第二个参数是data 传过来的数据
            // this.pubId= pubsub.subscribe('hello',function(msgName,data){
            //     console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
            //     // 这里面的this是undefined
            // })
            this.pubId= pubsub.subscribe('hello',(msgName,data)=>{
                console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
                // 这里面的this是vc
            })
        },
        beforeDestroy() {
            // 当我们的school组件销毁前,我们取消订阅
            // 这个取消订阅的方式很像定时器,传入一个id取消
            pubsub.unsubscribe(this.pubId)
        },
  
        }
</script>

<style scoped>
     .school{
        background-color: skyblue;
        padding:5px;
        margin-top:30px;
     }
</style>



  重要代码

        import pubsub from 'pubsub-js'
        mounted() {
            // 订阅hello消息
            // function(msgName,data) 第一个参数是msgName 消息名  第二个参数是data 传过来的数据
            // this.pubId= pubsub.subscribe('hello',function(msgName,data){
            //     console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
            //     // 这里面的this是undefined
            // })
            this.pubId= pubsub.subscribe('hello',(msgName,data)=>{
                console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
                // 这里面的this是vc
            })
        },
        beforeDestroy() {
            // 当我们的school组件销毁前,我们取消订阅
            // 这个取消订阅的方式很像定时器,传入一个id取消
            pubsub.unsubscribe(this.pubId)
        },
  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱布朗熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值