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

阳光穿透心脏的1/2处 2024-04-07 15:36 132阅读 0赞

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

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

目录

总结

工作模型

下载库

引入库

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

student.vue发布消息

重要代码

school.vue接收消息

重要代码


总结

350d5e9128144bbfbba21b6d3036b221.png

工作模型

751762e561a44bdd9d64c958dfcd17e7.png

下载库

3f48d2a6ed5b4ea5b8650782f1632f69.png

引入库

  1. import pubsub from 'pubsub-js'

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

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

student.vue发布消息

  1. <template>
  2. <!-- <template>标签不参与编译,在页面展现的是下面的一段结构 -->
  3. <!-- 组件结构 -->
  4. <div class="student">
  5. <h2 > 学生名称:{
  6. {name}}</h2>
  7. <h2> 学生性别:{
  8. {sex}}</h2>
  9. <button @click="sendStudentName">把学生名给School组件</button>
  10. </div>
  11. </template>
  12. <script>
  13. import pubsub from 'pubsub-js'
  14. export default {
  15. name:'Student',
  16. data(){
  17. return{
  18. name:'张三',
  19. sex:'男',
  20. age:18
  21. }
  22. },
  23. methods:{
  24. sendStudentName(){
  25. // 发布hello消息 带着数据
  26. pubsub.publish('hello',this.name)
  27. }
  28. }
  29. }
  30. </script>
  31. <style scoped>
  32. .student{
  33. background-color: red;
  34. padding:5px;
  35. margin-top:30px;
  36. }
  37. </style>

重要代码

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

school.vue接收消息

  1. <template>
  2. <div class="school">
  3. <h2> 学校名称:{
  4. {name}}</h2>
  5. <h2> 学校地址:{
  6. {address}}</h2>
  7. </div>
  8. </template>
  9. <script>
  10. import pubsub from 'pubsub-js'
  11. export default {
  12. name:'School',
  13. data(){
  14. return{
  15. name:'尚硅谷',
  16. address:'北京昌平',
  17. }
  18. },
  19. mounted() {
  20. // 订阅hello消息
  21. // function(msgName,data) 第一个参数是msgName 消息名 第二个参数是data 传过来的数据
  22. // this.pubId= pubsub.subscribe('hello',function(msgName,data){
  23. // console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
  24. // // 这里面的this是undefined
  25. // })
  26. this.pubId= pubsub.subscribe('hello',(msgName,data)=>{
  27. console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
  28. // 这里面的this是vc
  29. })
  30. },
  31. beforeDestroy() {
  32. // 当我们的school组件销毁前,我们取消订阅
  33. // 这个取消订阅的方式很像定时器,传入一个id取消
  34. pubsub.unsubscribe(this.pubId)
  35. },
  36. }
  37. </script>
  38. <style scoped>
  39. .school{
  40. background-color: skyblue;
  41. padding:5px;
  42. margin-top:30px;
  43. }
  44. </style>

重要代码

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

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用发布订阅PubSub

    一、PubSub消息传递模式介绍 发布-订阅(Publish-Subscribe, PubSub)是一种历史悠久的经典消息传递模式。在发布-订阅模式中,想要发布事件(eve