组件通信-父子组件通信

126 阅读1分钟

1、$children

使用this.$children查找当前组件的直接子组件。

console.log(this.$children[0].message)

2、$refs

使用this.$refs查找命名子组件,在子组件中加key,类似于id和class的用法。

<template>
  <div>
    <cpn ref="aaa"></cpn>
  </div>
</template>
console.log(this.$refs.aaa)
console.log(this.$refs.aaa.message)

3、$parent

使用this.$parent查找当前组件的父组件

console.log(this.$parent.message)

4、$root

使用this.$root查找根组件

console.log(this.$root)

可以结合$children使用

console.log(this.$root.$children[0])