欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

消息订阅与发布

1.订阅消息(需要数据的人):消息名称
2.发布消息(提供数据的人):消息内容
注:
1.订阅名称和发布名称一致
2.需要数据的人--订阅消息;提供数据的人--发布消息

 

示例一:
第一步:安装支持库
安装第三方支持库:pubsub.js(退出项目运行后执行以下命令,安装库文件)
npm i pubsub-js 
第二步:引入 + 订阅
在需要数据的组件引入组件pubsub-js 
import pubsub from 'pubsub-js';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!-- 组件的结构 -->
<template>
  <div class="school">
    <h3>学校姓名:{{name}}</h3>
    <h3>学校地址:{{ address }}</h3>
 
  </div>
</template>
 
<!-- 组件交互相关的代码(数据、方法等) -->
<script>
// 引入消息订阅发布插件
import pubsub from 'pubsub-js';
 
export default ({
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'School',
  data () {
    return {
      name: '高新一小',
      address: '西安/高新一小'
    }
  },
  methods: {
    demo (msgName, getdata) {
      console.log(this)
      console.log('School 发布了hello消息,hello消息的回调执行了==>', msgName, getdata)
    }
  },
  mounted () {
    /*     // 订阅消息
        // this.pubId = pubsub.subscribe('hello', function (msgName, getdata) {
        this.pubId = pubsub.subscribe('hello', (msgName, getdata) => {
          console.log(this)
          console.log('School 发布了hello消息,hello消息的回调执行了==>', msgName, getdata)
        }) */
 
 
    this.pubId = pubsub.subscribe('hello', this.demo)
  },
  beforeDestroy () {
    pubsub.unsubscribe(this.pubId)
  },
})
</script>
 
<!-- 组件的样式 scoped局部样式,否则多个vue组件中同名会导致样式覆盖(将使用最后一个引入的组件样式)-->
<style scoped>
.school {
  background-color: burlywood;
}
</style>

第三步:引入 + 发布

// 引入消息订阅发布插件
import pubsub from 'pubsub-js';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!-- 组件的结构 -->
<template>
  <div class="student">
    <h3>学生姓名:{{name}}</h3>
    <h3>学生性别:{{ sex }}</h3>
    <button @click="sendStudentName">把学生姓名给School组件</button>
  </div>
</template>
 
<!-- 组件交互相关的代码(数据、方法等) -->
<script>
// 引入消息订阅发布插件
import pubsub from 'pubsub-js';
 
export default ({
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Student',
  data () {
    return {
      msg: 'Vue消息发布与订阅',
      name: '心仪',
      sex: '女',
    }
  },
  methods: {
    sendStudentName () {
      pubsub.publish('hello', this.msg)
    }
  },
  mounted () {
 
  },
 
})
</script>
 
<!-- 组件的默认样式 css写法 -->
<!-- <style scoped>
.demo {
  background-color: cadetblue;
}
</style> -->
 
<style lang="less" scoped>
.student {
  background-color: cadetblue;
  .myfontsize {
    font-size: 40px;
  }
}
</style>

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 引入Vue
import Vue from 'vue'
// 引入App
import App from './App.vue'
// 配置提示
Vue.config.productionTip = false
 
// console.log('Vue.prototype==>', Vue.prototype)
// console.log('VueComponent.prototype.__proto__==>', Vue.prototype)
 
new Vue({
  render: h => h(App),
}).$mount('#app')
 
 
/*
消息订阅与发布
1.订阅消息(需要数据的人):消息名称
2.发布消息(提供数据的人):消息内容
注:
1.订阅名称和发布名称一致
2.需要数据的人--订阅消息;提供数据的人--发布消息
 
第三方支持库:pubsub.js
 */

App.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<template>
  <div class="app">
    <!-- <img src="./assets/logo.png"> -->
    <h2>{{msg}}</h2>
    <hr>
    学校组件:
    <School />
    <hr>
    学生组件:
    <Student />
  </div>
</template>
 
<script>
// 引入组件
import Student from './components/Student.vue';
import School from './components/School.vue';
export default {
  name: 'App',
  components: { Student, School },
  data () {
    return {
      msg: '消息订阅与发布'
    }
  },
 
}
</script>
 
<style scoped>
.app {
  background-color: rgb(178, 168, 168);
  padding: 15px;
}
</style>

  

posted on   sunwugang  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示