关于组件传值方法

  1. 父传子
  2. 子传父
  3. eventbus
  4. ref/$refs
  5. $ parent/$children
  6. $ attrs/$listeners
  7. 依赖注入(provide/inject)
  8. vuex

1. 父传子

父组件是通过props属性给子组件通信的

数据是单向流动 父—>子 (子组件中修改props数据,是无效的)

父组件内容

 <template>
   <div class="parent">
     <son :father="father"></son> 
 </div>
 </template>
 <script>
 import son from './Son' //引入子组件
 export default {
   name: 'Father',
   components:{son},
   data () {
     return {
       father: '我是父组件',
     }
   },
 }
 </script>

子组件内容

<template>
   <div class="son">
     <p>{{ father }}</p>
   </div>
 </template>
 <script>
 export default {
   name: "son",
   props:['father'],
 }
</script>

2. 子传父

子组件

<template>
<div>
  <span>{{son}}</span>
  <!-- 定义一个子组件传值的方法 -->
  <input type="button" value="点击触发" @click="childClick">
</div>
</template>
<script>
  export default {
    data () {
      return {
        son: '我是子组件的数据'
      }
    },
    methods: {
      childClick () {
        // childByValue是在父组件on监听的方法
        // 第二个参数this.childValue是需要传的值
        this.$emit('getSon', this.son)
      }
    }
  }
</script>

父组件

<template>
   <span>{{name}}</span>
   <!-- 引入子组件 定义一个on的方法监听子组件的状态-->
   <son @getSon="getSon"></son>
 </template>
<script>
  import son from './son'
  export default {
    components: {
      son
    },
    data () {
      return {
        name: ''
      }
    },
    methods: {
      getSon(getSon) {
        // childValue就是子组件传过来的值
        this.name = getSon
      }
    }
  }
</script>

3. eventbus

main.js 初始化 EventBus

// main.js
Vue.prototype.$EventBus = new Vue()

新创建一个 .js 文件,比如 bus.js

import Vue from 'vue'
export const EventBus = new Vue()

调用
A页面

<template>
    <button @click="sendMsg()">发送</button>
</template>
 
<script> 
import { EventBus } from "../bus.js";
export default {
  methods: {
    sendMsg() {
      EventBus.$emit("Msg", '来自A页面的消息');
    }
  }
}; 
</script>

B页面

<script> 
import { 
  EventBus 
} from "../bus.js";
export default {
    mounted() {
    EventBus.$on("Msg", (msg) => {
      // A发送来的消息msg
    });
  }
};

4. ref/$refs

子组件

//子组件
 
<template>
    <div>{{msg}}</div>
</template>
<script>
export default {
  data() {
    return {
      msg: '我是子组件'
    }
  },
  methods: {
    getMsg() {
      this.msg = '点我重新赋值'
    }
  }
}
</script>

父组件

//父组件
 
<template>
  <div @click="getData">
    <son ref="son"></children>
  </div>
</template>
<script>
import son from 'components/son.vue'
export default {
  components: { 
    son 
  },
  methods: {
    getData() {
      //返回一个对象
      this.$refs.son  
      //调用son的getMsg方法
      this.$refs.son.getMsg() 
    }
  }
}
</script>

5. $ parent/$children

父传子

注意:在钩子方法mounted中无法获取到父组件中的数据和方法
父组件

<template>
  <div>
    <h1>父组件</h1>
    <son></son>
  </div>
</template>
<script>
  // 引入子组件
  import son from './son'
  export default {
    name: 'Parent',
    components: {
      son
    },
    data () {
      return {
        msg: 'data from parent'
      }
    },
    methods: {
      fun () {
        console.log('parent fun')
      }
    }
  }
</script>

子组件

<template>
  <div>
    <h1>子组件</h1>
    <button @click="showParent">调用父组件的数据和方法</button>
  </div>
</template>
<script>
  export default {
    name: 'Child',
    methods: {
      showParent () {
        // 获取到所有的子组件
        console.log(this.$parent)
        // 获取指定子组件中的指定数据
        console.log(this.$parent.msg)
        // 调用子组件的方法
        this.$parent.fun()
      }
    }
  }
</script>

子传父

子组件

<template>
  <div>
    <h1>子组件</h1>
  </div>
</template>
<script>
  export default {
    name: 'son',
    data () {
      return {
        msg: 'msg from child'
      }
    },
    methods: {
      fun () {
        console.log('child fun')
      }
    }
  }
</script>

父组件

<template>
  <div>
    <h1>父组件</h1>
    <son></son>
  </div>
</template>
<script>
  // 引入子组件
  import son from './son'
  export default {
    name: 'Parent',
    components: {
      son
    },
    mounted () {
      // 获取到所有的子组件,结果是一个数组
      console.log(this.$children)
      // 获取指定子组件中的指定数据
      console.log(this.$children[0].msg)
      // 调用子组件的方法
      this.$children[0].fun()
    }
  }
</script>

6. $ attrs/$listeners

可以看到无论是在Son组件中还是GrandSon组件中都可以拿到$attrs中的值。

子组件拿父组件,爷爷组件

$attrs:

//GrandFather.vue
<template>
  <div>
    <span>爷爷</span>
    <father :message="message" :age="age"></father>
  </div>
</template>
<script>
import father from "../father.vue";
export default {
components: {
    father,
  },
  data() {
    return {
      message: "我是爷爷组件",
      age: 24,
    };
  },
};
</script>

从孙子组件发送事件到父子组件中。

//GrandFather.vue
<template>
  <div >
    <span>爷爷</span>
    <Father @event="event"></Father>
  </div>
</template>
<script>
import Father from "../Father.vue";
export default {
  components: {
    Father,
  },
  methods: {
    event() {
      console.log("从Son组件发送过来的数据");
    },
  },
};
</script>

//Father.vue
<template>
  <div >
    <span>Father</span>
    <Son v-on="$listeners"></Son>
  </div>
</template>
<script>
import Son from "../Son.vue";
export default {
  components: {
    Son,
  },
};
</script>

//Son.vue
<template>
  <div >
    <span>儿子</span>
    <button @click="click">发送事件</button>
  </div>
</template>
<script>
export default {
  methods: {
    click() {
      this.$emit("event");
    },
  },
};
</script>

7. 依赖注入(provide/inject)

provide和inject只能从父组件向孙组件传值,不能从孙组件向父组件传值

官网介绍
在这里插入图片描述
在这里插入图片描述

export default {
  name: "App",
  components: {
    Parent
  },
  data() {
    return {
      msg: '我是父组件',
      age: 12
    }
  },
  provide() {
    return {
    msg: this.msg,
    age: this.age,
  }
 }
}

子组件或者孙子…

export default {
  name: 'Son',
  components: {
    Son
  },
  inject: ['msg', 'age',]
  data() {
    return {
      sonMsg: this.msg
    }
  },
  created() {
    console.log(this.msg, this.age 'inject数据');
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值