Vue很重要的一点就是组件化开发,那么如果在两个组件之间通信,即在两个组件之间传输数据呢?可以分为两部分来看

  1. 父组件如何传递信息给子组件
  2. 子组件如何传递消息给父组件

1. 父组件传递数据给子组件

通过props的方式实现

父组件可以直接通过props来向子组件传输数据
父组件:

  1. <template>
  2. <div id="app">
  3. <child :names="names" v-on:msgChange="msgChange"/>
  4. </div>
  5. </template>
  6. <script>
  7. import Child from './components/Child.vue'
  8. export default {
  9. name: 'App',
  10. components: {
  11. Child
  12. },
  13. data() {
  14. return {
  15. names: ['Jenly','Hary','Kook','Hnony']
  16. }
  17. },
  18. methods: {
  19. msgChange(val){
  20. console.log('val:', val);
  21. }
  22. },
  23. }
  24. </script>
  25. <style>

子组件:

  1. <template>
  2. <div>
  3. <div v-for="(item,index) in names" :key="index">{{ item }}</div>
  4. <input type="text" v-model="msg">
  5. <button @click="handleMsgChange">点击发送</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'Child',
  11. data() {
  12. return {
  13. msg: ''
  14. }
  15. },
  16. props: {
  17. names: {
  18. required: true,
  19. type: Array
  20. }
  21. },
  22. methods: {
  23. handleMsgChange(){
  24. this.$emit('msgChange',this.msg);
  25. }
  26. },
  27. }
  28. </script>
  29. <style scoped>
  30. </style>

2. 子组件传递数据给父组件

子组件向父组件传输数据的方式是通过事件的方式

可以使用this.$emit来通过绑定的事件向父组件发送消息,实际就是把子组件自己的数据发送给父组件

  1. <template>
  2. <div id="app">
  3. <child :names="names" v-on:msgChange="msgChange"/>
  4. </div>
  5. </template>
  6. <script>
  7. import Child from './components/Child.vue'
  8. export default {
  9. name: 'App',
  10. components: {
  11. Child
  12. },
  13. data() {
  14. return {
  15. names: ['Jenly','Hary','Kook','Hnony']
  16. }
  17. },
  18. methods: {
  19. msgChange(val){
  20. console.log('child val:', val);
  21. }
  22. },
  23. }
  24. </script>
  25. <style>
  1. <template>
  2. <div>
  3. <div v-for="(item,index) in names" :key="index">{{ item }}</div>
  4. <input type="text" v-model="msg">
  5. <button @click="handleMsgChange">点击发送</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'Child',
  11. data() {
  12. return {
  13. msg: ''
  14. }
  15. },
  16. props: {
  17. names: {
  18. required: true,
  19. type: Array
  20. }
  21. },
  22. methods: {
  23. handleMsgChange(){
  24. this.$emit('msgChange',this.msg);
  25. }
  26. },
  27. }
  28. </script>
  29. <style scoped>
  30. </style>

3. 使用订阅和发布来获取消息

如果A组件想要收到来自B组件的消息,那么可以在A组件中订阅消息,在B组件中收到消息,订阅的回调存在A组件中,并且回调最好使用箭头函数的形式

app.vue

  1. <template>
  2. <div id="app">
  3. <student />
  4. <school />
  5. </div>
  6. </template>
  7. <script>
  8. import School from './components/School.vue'
  9. import Student from './components/Student.vue'
  10. export default {
  11. name: 'App',
  12. components: {
  13. School,
  14. Student
  15. },
  16. data() {
  17. return {
  18. names: ['Jenly','Hary','Kook','Hnony']
  19. }
  20. },
  21. methods: {
  22. },
  23. }
  24. </script>
  25. <style>

student.vue

  1. <template>
  2. <div>
  3. <div @click="showName">姓名:{{ name }}</div>
  4. <div>性别:{{ gender }}</div>
  5. <div>年龄:{{ age }}</div>
  6. </div>
  7. </template>
  8. <script>
  9. import {mixin} from '../mixin';
  10. import pubsub from 'pubsub-js';
  11. export default {
  12. name: 'School',
  13. data() {
  14. return {
  15. name: 'yxr',
  16. gender: '男',
  17. age: 18
  18. }
  19. },
  20. mixins: [mixin],
  21. mounted() {
  22. this.pubId = pubsub.subscribe('school_msg',(name,val) => {
  23. console.log('student收到消息,消息名为:', name, '消息内容为:', val);
  24. })
  25. },
  26. beforeDestroy() {
  27. pubsub.unsubscribe(this.pubId);
  28. },
  29. }
  30. </script>

school.vue

  1. <template>
  2. <div class="demo">
  3. <h2 @click="showName" class="name">学校名称:{{ name }}</h2>
  4. <h2>学校地址:{{ address }}</h2>
  5. <input type="text" v-model="msg">
  6. <button @click="sendMsg">点击发送消息</button>
  7. </div>
  8. </template>
  9. <script>
  10. import pubsub from 'pubsub-js';
  11. import {mixin} from '../mixin';
  12. export default {
  13. name: 'School',
  14. data() {
  15. return{
  16. name: '三峡大学',
  17. address: '宜昌市',
  18. msg: ''
  19. }
  20. },
  21. methods: {
  22. sendMsg(){
  23. pubsub.publish('school_msg','xx学生你好!');
  24. }
  25. },
  26. mixins: [mixin]
  27. }
  28. </script>
  29. <style lang="less">
  30. .demo{
  31. background-color: aqua;
  32. .name{
  33. color: red
  34. }
  35. }
  36. </style>