专业的编程技术博客社区

网站首页 > 博客文章 正文

vue通过ref实现子父通信(vue父子组件如何通信)

baijin 2024-10-04 13:25:00 博客文章 6 ℃ 0 评论

一 子组件

<template>
  <div>
  {{ msg }}
    </div>
</template>
<script>
      export default {
data() {
  return {
    msg: "hello world~~~"
  }
}
}
  </script>

二 父组件

<template>
  <div id="app">
    <!-- ref的值任意写,只是一个标识,父组件接收子组件的值的时候对应好就行了 -->
    <hello-world ref="ree"></hello-world>
<button @click="getHello">获取helloworld组件中的值</button>
</div>
</template>
<script>
      import HelloWorld from "./components/HelloWorld.vue";
export default {
  components: {
    HelloWorld
  },
  methods: {
    getHello() {
      console.log(this.$refs.ree.msg)
    }
  }
};
</script>

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表