在子組件中觸發事件,傳值給父組件-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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM