在子组件中触发事件,传值给父组件-vue

1、通过$emit触发事件
在子组件<x-test>中触发事件:
<button @click="toSearchProduct()">搜索</button>
export default {
  methods: {
    show: function () {
      console.log(this.name)
    },
    toSearchProduct: function () {
      this.$emit('parentEvent','哈哈啊哈哈')
    }
  }
}
父组件:
<x-test :name="username" @parentEvent="toClick"></x-test>
export default {
  components: {
    XTest
  },
  methods: {
    toClick: function (msg){
      console.log(msg) // 当子组件触发按钮时,msg获取值为 哈哈啊哈哈
    }
  }
}
</script>
原文地址:https://www.cnblogs.com/yuyedaocao/p/11981698.html