Vue组件通信(父子组件传值,非父子组件传值)

电玩女神 2023-06-23 02:56 263阅读 0赞

主要是整理给自己用的一些面试常用题,这里记录一下vue组件间通信几种方式。

父组件传值到子组件(父传子)

先定义一个子组件,这里我在component文件夹里定义一个child.vue

子组件

  1. <template>
  2. <div class="about">
  3. <h1>这里是子组件的变量接受的变量{
  4. {msg}}</h1>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. msg: Number
  11. }
  12. }
  13. </script>

父组件

  1. <template>
  2. <div class="home">
  3. <div>我是父组件的变量{
  4. {num}}</div>
  5. <button @click="add">父组件变量+1</button>
  6. <Child :msg="num" @getData="getData"></Child>
  7. </template>
  8. <script>
  9. import Child from '@/components/child.vue'
  10. export default {
  11. data() {
  12. return {
  13. num: 18
  14. }
  15. },
  16. components: {
  17. Child
  18. },
  19. methods: {
  20. add() {
  21. this.num++
  22. }
  23. }
  24. }
  25. </script>

实现效果
在这里插入图片描述
父组件传子组件还是比较容易理解的,实现方式就是通过props属性,子组件通过props属性接收从父组件传过来的数据,而父组件传值给子组件的时候使用 v-bind 将子组件中props预留的变量名绑定到父组件data里面的数据就好了,记得你在子组件定义的数据类型

子组件传值到父组件(子传父)

子组件代码

  1. <template>
  2. <div class="about">
  3. <div>
  4. <input type="text" v-model="inputMsg">
  5. </div>
  6. <button @click="setData">把输入的文字传递给父组件</button>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. inputMsg: ''
  14. }
  15. },
  16. methods: {
  17. setData() {
  18. this.$emit("setData", this.inputMsg);
  19. }
  20. }
  21. }
  22. </script>

父组件代码

  1. <template>
  2. <div class="home">
  3. <div class="red">父组件默认:{
  4. {msg}}</div>
  5. <Child @setData="getData"></Child>
  6. </template>
  7. <script>
  8. import Child from '@/components/child.vue'
  9. export default {
  10. data() {
  11. return {
  12. msg: '父组件默认数据'
  13. }
  14. },
  15. components: {
  16. Child
  17. },
  18. methods: {
  19. getData(data) {
  20. this.msg = data;
  21. }
  22. }
  23. }
  24. </script>

在这里插入图片描述
子组件传递数据到父组件,子组件通过this.$emit自定义事件向父组件传递信息,父组件监听子组件的事件来接收

非父子组件传值

方法一:

新建一个bus.js文件, 在这个文件里实例化一下vue;然后在组件A和组件B中分别引入这个bus.js文件,将事件监听和事件触发都挂到bus.js这个实例上,这样就可以实现全局的监听与触发了。

bus.js文件

  1. //bus.js
  2. import Vue from 'vue'
  3. export default new Vue()

组件A

  1. <template>
  2. <div>
  3. A组件:
  4. <button @click="AValueChange">传值给B组件</button>
  5. <span>{
  6. {AValue}}</span>
  7. </div>
  8. </template>
  9. <script>
  10. // 引入公共的bug,来做为中间传达的工具
  11. import Bus from './bus.js'
  12. export default {
  13. data () {
  14. return {
  15. AValue: 4
  16. }
  17. },
  18. mounted () {
  19. var that = this
  20. // 用$on事件来接收参数
  21. // Bus.$on("事件名称",事件处理函数)写在mounted钩子函数里面,事件处理函数里面就写当这个触发事件的回调函数里面,应该干的事。
  22. Bus.$on('val', (data) => {
  23. that.AValue = data
  24. })
  25. },
  26. methods: {
  27. // 触发事件
  28. AValueChange() {
  29. Bus.$emit('val', this.AValue)
  30. }
  31. }
  32. }
  33. </script>

组件B

  1. <template>
  2. <div>
  3. B组件:
  4. <button @click="getData">自身+1</button>
  5. <button @click="BValueChange">传值给A组件</button>
  6. <span>{
  7. {BNum}}</span>
  8. </div>
  9. </template>
  10. <script>
  11. import Bus from './bus.js'
  12. export default {
  13. data () {
  14. return {
  15. BNum: 0
  16. }
  17. },
  18. mounted () {
  19. var that = this
  20. // 用$on事件来接收参数
  21. Bus.$on('val', (data) => {
  22. that.BNum = data
  23. })
  24. },
  25. methods: {
  26. getData() {
  27. this.BNum++
  28. },
  29. BValueChange() {
  30. Bus.$emit('val', this.BNum)
  31. }
  32. }
  33. }
  34. </script>

在这里插入图片描述

方法二:

在main.js,也就是入口文件中,在vue的原型上添加一个bus对象,原理跟方法一类似,只不过更加简单

  1. //在main.js中
  2. Vue.prototype.bus = new Vue()
  3. // 传递组件里的触发事件
  4. this.bus.$emit('updata', this.AValue)
  5. // 接收组件里面的监听事件
  6. this.bus.$on('updata', (data) => {
  7. console.log(data) //data就是触发updata事件带过来的数据
  8. })

这里就不上详细代码了,因为在方法一里面改下即可。
A组件里面的触发事件

  1. methods: {
  2. // 传递组件里的触发事件
  3. AValueChange() {
  4. this.bus.$emit('updata', this.AValue)
  5. },
  6. }

B组件里面的mounted函数里面的监听事件

  1. mounted() {
  2. var that = this
  3. // 接收组件里面的监听事件
  4. this.bus.$on('updata', (data) => {
  5. console.log(data) //data就是触发updata事件带过来的数据
  6. that.BNum = data
  7. })
  8. }

实现效果:
在这里插入图片描述
这里就是改了下,原理还是类似

其实这些可以直接Vuex状态管理工具来实现,看实际应用场景。

本来是这些都在一个demo里面的,便于理解写的时候把这些拆分出来了。复制代码的时候可能会出现些问题,如果各位发现问题了,可以指出,我加以更正,感谢。

个人水平有限,有问题欢迎大家留言指导,仅供学习和参考。

学海无涯!努力二字,共勉!

发表评论

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

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

相关阅读

    相关 Vue父子组件

    Vue父子组件传值:有四种方式:props,ref,emit 和模板传递通信slot 通过props来传值: 静态传值就是直接通过props来传递 动态传值是通