vue如何获取子组件的实例,方法和数据

利用ref指向获取子元素实例

  1. ref放在普通标签上,拿到的就是普通标签本身

  1. ref放在子组件上,拿到的就是子组件对象

  1. -通过这种方式实现子传父(this.$refs.mychild.text)

  1. -通过这种方式实现父传子(调用子组件方法传参数)

<template>
  <div>
    <son ref="son" />
  </div>
</template>

<script>
import son from '@/components/SonFile.vue'

export default {
  components: {
    son
  },
  mounted() {
      console.log(this.$refs.son,"我是子组件的实例")
  },
}
</script>

猜你喜欢

转载自blog.csdn.net/m0_60322614/article/details/128671985