Vue组件之间的两种常用通信方式

灰太狼 2023-06-12 04:22 54阅读 0赞

最近都一直学习Vue的知识,看了不少视频,之前完全不了解前端知识,所以还是遇到很多困难,
用惯了强类型语言来看js的代码真的有种感觉随笔写都不影响编译的感觉。
今天总结一下Vue中的组件传值问题。这里使用的demo直接vue init webpack-simple **** 命令初始化的。

父子组件通信

父组件传递给子组件多重类型
在这里插入图片描述
我们可以在props中做类型检测,下面看父子组件的代码。
父组件通过传值,子组件直接调用或者emit调用父组件的方法,父组件也可以直接拿到子组件的引用,直接使用子组件的值或者方法。

  1. <template>
  2. <div>
  3. <h2>子组件</h2>
  4. <input type="button" @click="log" value="click">
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "Child",
  10. data() {
  11. return {
  12. ownMsg: '子组件的msg'
  13. }
  14. },
  15. methods: {
  16. childLog() {
  17. console.log('子组件Log')
  18. },
  19. log() {
  20. console.log(this.passMsg)
  21. console.log(this.obj)
  22. //这是传过来的parent
  23. this.parent.change()
  24. //这是直接获取
  25. this.$parent.change()
  26. this.func1()
  27. this.$emit('func2')
  28. }
  29. },
  30. props: {
  31. passMsg: {
  32. type: String,
  33. },
  34. obj: {
  35. type: Object,
  36. },
  37. parent: {
  38. type: Object
  39. },
  40. func1: {
  41. type: Function
  42. }
  43. }
  44. }
  45. </script>
  46. <style scoped>
  47. </style>

父组件

  1. <template>
  2. <div>
  3. <h1>父组件</h1>
  4. <h2> {
  5. {
  6. msg }}</h2>
  7. <input type="button" @click="childFunc" value="click">
  8. <Child
  9. ref="child"
  10. passMsg="msg"
  11. :func1="change"
  12. :obj="obj"
  13. :parent="this"
  14. @func2="change"
  15. >
  16. </Child>
  17. </div>
  18. </template>
  19. <script>
  20. import Child from "./Child";
  21. export default {
  22. name: "Parent",
  23. data() {
  24. return {
  25. msg: "我是父组件的msg",
  26. obj: {
  27. name: "gg",
  28. age: 15
  29. }
  30. }
  31. },
  32. methods: {
  33. change() {
  34. console.log('父组件log')
  35. },
  36. childFunc(){
  37. const c = this.$refs.child
  38. c.childLog()
  39. console.log(c.ownMsg)
  40. }
  41. },
  42. components: {
  43. Child},
  44. comments: {
  45. Child
  46. }
  47. }
  48. </script>
  49. <style scoped>
  50. </style>

非父子组件通信

非父子组件传值就有点类似于安卓中使用的EventBus了,我们首先准备一个event.js

  1. import Vue from 'vue'
  2. let VueEvent = new Vue()
  3. export default VueEvent

新建一个兄弟组件,Brother.vue,这里点击按钮会发送内容到一个事件中去,如果有监听,就会触发对应的方法。

  1. <template>
  2. <div>
  3. <h2>兄弟组件</h2>
  4. <input type="input" value="click" v-model="ownMsg">
  5. <input type="button" value="click" @click="send">
  6. </div>
  7. </template>
  8. <script>
  9. import VueEvent from "../model/event";
  10. export default {
  11. name: "Brother",
  12. data() {
  13. return {
  14. ownMsg: '兄弟的msg'
  15. }
  16. },
  17. methods: {
  18. send() {
  19. VueEvent.$emit('to_msg', this.ownMsg)
  20. }
  21. }
  22. }
  23. </script>
  24. <style scoped>
  25. </style>

再看看Child.vue

  1. <template>
  2. <div>
  3. <h2>子组件</h2>
  4. <input type="button" @click="log" value="click">
  5. </div>
  6. </template>
  7. <script>
  8. import VueEvent from "../model/event";
  9. export default {
  10. name: "Child",
  11. data() {
  12. return {
  13. ownMsg: '子组件的msg'
  14. }
  15. },
  16. methods: {
  17. childLog() {
  18. console.log('子组件Log')
  19. },
  20. log() {
  21. console.log(this.passMsg)
  22. console.log(this.obj)
  23. //这是传过来的parent
  24. this.parent.change()
  25. //这是直接获取
  26. this.$parent.change()
  27. this.func1()
  28. this.$emit('func2')
  29. }
  30. },
  31. props: {
  32. passMsg: {
  33. type: String,
  34. },
  35. obj: {
  36. type: Object,
  37. },
  38. parent: {
  39. type: Object
  40. },
  41. func1: {
  42. type: Function
  43. }
  44. },
  45. mounted() {
  46. VueEvent.$on('to_msg', function (data) {
  47. console.log(data);
  48. })
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. </style>

最后这个方法就会打印对应日志,事件总线的形式。
这里简单介绍了两种,更详细的大家可以看看这个地址,基本罗列了各种方法。

Vue 组件通信方式全面详解

发表评论

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

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

相关阅读

    相关 vue之间通信

    作为一个vue初学者不得不了解的就是组件间的数据通信(暂且不谈vuex)。通信方式根据组件之间的关系有不同之处。组件关系有下面三种:父-->子、子-->父、非父子 父-->子