首页 > 解决方案 > 修改子组件中的选择值后触发函数 - VueJs

问题描述

我正在使用 VueJs,并且我的父组件中有这个子组件,当检测到子组件的选择发生变化时,我需要在父组件中触发一个函数。

子组件:

watch: {
    selectCoin() {
      this.$emit("currencyType", this.selectCoin["title"]);
    }
}

我的父亲组件中的子组件:

<app-select-coin
  @coin="body.value = $event"
  @currencyType="body.currencyType = $event"
  :data="body"
/>

当子组件响应父组件的 $emit 时,我需要调用此方法:

methods :{
   myFunction() {

   }
}

标签: vue.jsvue-componentvue-props

解决方案


我设法通过在@currencyType 的“v-on”中添加函数,并将我从“$emit”中的子项收到的值作为参数传递来做到这一点。:

<app-select-coin
  @coin="body.value = $event"
  @currencyType="myFunction(body.currencyType = $event)"
  :data="body"
/>
methods :{
   myFunction(ct) {

   }
}

推荐阅读