$emit 在计算之前执行

Ama*_*mar 1 javascript data-binding vue.js

我正在尝试发出一个我修改过的道具。我可以直接使用 prop 来做到这一点,它可以工作,但我收到一个 Vue 警告:Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders

因此,我尝试将 prop 放入计算中,但在执行计算之前执行发射。

template:
          <input
                v-on:keyup="validatedate(localDate)"
                v-on:change="emitAnswer(localDate)"
                v-model="localDate">
 ,
 computed: {
    dateLocal: {
        get: function () {
            return this.date
        }
 }
 methods: {
     emitAnswer: function (date) {
        this.$emit('myFunc', date);
    }
 }
Run Code Online (Sandbox Code Playgroud)

Mar*_*elo 5

由于 Vue 无法保证 v-model 将在 v-on:change 之前执行,因此您可以使用观察者仅在组件中的属性值发生更改时调用emit,例如:

watch {
  date() {
     this.$emit('myFunc', this.date)
  }
}
Run Code Online (Sandbox Code Playgroud)