Vue组件间的通信

逃离我推掉我的手 2022-09-05 12:38 348阅读 0赞

下面的总结都将按照这个模型图去设计

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L29sZHNoYXVp_size_16_color_FFFFFF_t_70

父传子的方式 :(Props)

App组件:

  1. <template>
  2. <div>
  3. <Student name="carry" :age=26 sex="男"/>
  4. </div>
  5. </template>
  6. <script>
  7. import Student from './components/Student';
  8. export default {
  9. name:'App',
  10. components: {Student},
  11. }
  12. </script>
  13. <style lang='scss' scoped>
  14. </style>

Student组件

  1. <template>
  2. <div>
  3. <h1>{
  4. { msg }}</h1>
  5. <h2>学生姓名:{
  6. { name }}</h2>
  7. <h2>学生性别:{
  8. { sex }}</h2>
  9. <h2>学生年龄:{
  10. { myAge + 1 }}</h2>
  11. <button @click="updateAge">尝试修改收到的年龄</button>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. msg: "我是NewEgg",
  19. myAge: this.age,
  20. };
  21. },
  22. // 简单的声明接收
  23. //props:['name','age','sex'],
  24. //接收的同时,对数据类型进行限制
  25. // props:{
  26. // name:String,
  27. // age: Number,
  28. // sex: String
  29. // },
  30. //接收的同时,对数据类型限制+默认值的指定+必要性的限制
  31. props: {
  32. name: {
  33. type: String, //name的类型是字符串
  34. required: true, //name是必要的
  35. },
  36. age: {
  37. type: Number,
  38. default: 99, //默认值
  39. },
  40. sex: {
  41. type: String,
  42. required: true,
  43. },
  44. },
  45. components: {},
  46. computed: {},
  47. methods: {
  48. updateAge() {
  49. this.myAge++;
  50. },
  51. },
  52. };
  53. </script>
  54. <style lang='scss' scoped>
  55. </style>

总结:

父组件传给子组件的值,子组件利用Props去接收(几种情况可以看代码)

子组件给父组件传值Props方式

1.在父组件的定义:

  1. <!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
  2. <School :getSchoolName ="getSchoolName"/>

声明getSchoolName方法,接收子组件传递的数据

  1. methods:{
  2. getSchoolName(data) {
  3. console.log('我是App组件,我收到了我儿子School的数据',data)
  4. }
  5. }

2.在子组件

声明props接收:props:[‘getSchoolName’],

调用父组件的方法:

  1. methods:{
  2. sendAddrToApp() {
  3. // 父组件的方法
  4. this.getSchoolName(this.name)
  5. }
  6. }

子组件给父组件传递消息自定义事件的方式

1.在父组件定义:

或者:

在父组件的methods里面声明收到子组件传递的信息的方法

  1. methods:{
  2. sendNameToApp(name) {
  3. console.log('我是App组件,我收到了我儿子Student的数据',name)
  4. }
  5. }

2.在哪绑定事件就在哪去触发事件:

在子组件里面使用$emit去绑定事件

  1. methods:{
  2. sendStudentNameToApp() {
  3. this.$emit('sendNameToApp', this.name)
  4. }
  5. },

兄弟组件传递消息总线方式

School组件代码

  1. <template>
  2. <div class="school">
  3. <h2>学校名称:{
  4. {name}}</h2>
  5. <h2>学校地址:{
  6. {address}}</h2>
  7. <button @click="sendAddrToStu">把学校的地址给学生</button>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name:'School',
  13. data() {
  14. return {
  15. name:'南阳理工',
  16. address:'南阳',
  17. }
  18. },
  19. mounted() {
  20. this.$bus.$on('hello', (data) => {
  21. console.log('我是school组件,w我收到了Student数据', data)
  22. })
  23. },
  24. methods:{
  25. sendAddrToStu() {
  26. this.$bus.$emit('addr', this.address)
  27. }
  28. }
  29. }
  30. </script>
  31. <style scoped>
  32. .school{
  33. background-color: skyblue;
  34. padding: 5px;
  35. }
  36. </style>

Student组件代码

  1. <template>
  2. <div class="student">
  3. <h2>学生姓名:{
  4. {name}}</h2>
  5. <h2>学生性别:{
  6. {sex}}</h2>
  7. <button @click="sendStudentName">把学生名给School组件</button>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name:'Student',
  13. data() {
  14. return {
  15. name:'张三',
  16. sex:'男',
  17. number:0
  18. }
  19. },
  20. methods:{
  21. sendStudentName() {
  22. this.$bus.$emit('hello', this.name);
  23. }
  24. },
  25. mounted() {
  26. this.$bus.$on('addr',(data) => {
  27. console.log('我是stu组件,我收到了我兄弟组件传来的数据:',data)
  28. })
  29. }
  30. }
  31. </script>
  32. <style scoped>
  33. .student{
  34. background-color: pink;
  35. padding: 5px;
  36. margin-top: 30px;
  37. }
  38. </style>

App

  1. <!-- -->
  2. <template>
  3. <div>
  4. <h1>{
  5. { msg }},学生姓名是:{
  6. { studentName }}</h1>
  7. <School/>
  8. <Student/>
  9. </div>
  10. </template>
  11. <script>
  12. import Student from "./components/Student";
  13. import School from "./components/School";
  14. export default {
  15. name: "App",
  16. components: { Student, School },
  17. data() {
  18. return {
  19. msg: "你好啊!",
  20. studentName: "",
  21. };
  22. },
  23. };
  24. </script>
  25. <style scoped>
  26. </style>

main.js

  1. // 引入vue
  2. import Vue from 'vue'
  3. // 引入app
  4. import App from './App.vue'
  5. //关闭Vue的生产提示
  6. Vue.config.productionTip = false;
  7. //创VM
  8. new Vue({
  9. el:"#app",
  10. render: h=>h(App),
  11. beforeCreate() {
  12. Vue.prototype.$bus= this // 安装全局事件总线
  13. }
  14. })

利用消息订阅与发布实现任意组件的消息传递

1.引入第三方库: npm i pubsub-js

2.订阅消息一方:

  1. import pubsub from 'pubsub-js'
  2. mounted() {
  3. this.pubId = pubsub.subscribe('hello',(a,b) => {
  4. console.log('有人发布了hello消息,hello消息的回调执行啦',a,b)
  5. })
  6. //取消订阅
  7. beforeDestroy() {
  8. //取消订阅
  9. pubsub.unsubscribe(this.pubId)
  10. }

3.发布消息的一方

  1. import pubsub from 'pubsub-js'
  2. methods:{
  3. sendAddrToAppBySub() {
  4. pubsub.publish('hello', 666)
  5. }
  6. }

发表评论

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

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

相关阅读