kaorun343 / vue-property-decorator

Vue.js and Property Decorator
MIT License
5.52k stars 380 forks source link

@Emit: decorated fn emits key Event passed into function #164

Open maksnester opened 5 years ago

maksnester commented 5 years ago

I just refactored this method:

  onBackspaceKeyup() {
    if (someCondition) {
      this.$emit('input', someValue)
    } else if (yetAnotherCondition) {
      this.$emit('input', null)
    }
  }

into this:

  @Emit('input')
  onBackspaceKeyup() {
    if (someCondition) {
      return someValue
    }
    if (yetAnotherCondition) {
      return null
    }
    return undefined // expect no emit here at all
  }

And my component started to emit key event unexpectedly and it ruins everything. Method is used in event handler like this:

  <CustomComponent
        v-model="address"
        v-bind="attrsLocal"
        @keyup.native.delete="onBackspaceKeyup"
  />

Am I doing something wrong or it's just a bug?

I expected that no value would be emitted if undefined is returned from decorated function.

zhiquan-yu commented 5 years ago

@Alendorff If undefined is returned, no value would be emitted, code is here: https://github.com/kaorun343/vue-property-decorator/blob/master/src/vue-property-decorator.ts#L116.

Specifically to your problem, you should debug it with Vue devtools.

stephantabor commented 5 years ago

as far as i can tell, the decorated function will always emit and if undefined is returned from the decorated function it just won't emit the undefined, only the args to the decorated function

https://github.com/kaorun343/vue-property-decorator/blob/a4d384ea3423571fab175832232df28790dd9ad6/src/vue-property-decorator.ts#L127-L148

this discussion is relevant https://github.com/kaorun343/vue-property-decorator/issues/146