美文网首页Vue.jsVue.js专区
父组件点击触发子组件的事件

父组件点击触发子组件的事件

作者: Vivian_0430 | 来源:发表于2019-07-30 10:29 被阅读17次

点击父组件中的button调用子组件的事件,可用于父子组件的传值实时更新。
父组件代码:

<template>
<button @click="handleChange"></button>
<child ref="child"></child>
</template>
<script>
import child from "子组件相对父组件路径"
export default {
  components: {
    child
  }
  data () {
    return {
      msg: "",
    }
  }
  methods:{
    handleChange() {
      this.msg = "haha"
      this.$refs.child.parentMsg(this.msg);
    }
  }
}
</script>

子组件中代码:

<template>
</template>
<script>
export default {
  methods:{
    parentMsg(msg) {
      console.log(msg);  //haha
    }
  }
}
</script>

相关文章

网友评论

    本文标题:父组件点击触发子组件的事件

    本文链接:https://www.haomeiwen.com/subject/hulgrctx.html