docs icon indicating copy to clipboard operation
docs copied to clipboard

Explain different ways that props are treated in v3

Open pikax opened this issue 4 years ago • 2 comments

In v3 v-on was merged with props, for that to happen props started with on* are treated as events

<Comp 
        @myEvent="log('myEvent')" 
        @my-event="log('my-event')"
        :onMyEvent="()=>log('onMyEvent')"
  />
  

<! -- CONVERTED TO  --> 
h(Comp, {
      onMyEvent: [
        _cache[0] || (_cache[0] = $event => (log('myEvent'))),
        _cache[1] || (_cache[1] = $event => (log('my-event'))),
        ()=>log('onMyEvent')
      ]
})

playground

You can also declare events as props

defineComponent({
  props: {
    onClick: Function, // similar to emit: ['click']
  }
})

pikax avatar Oct 07 '21 13:10 pikax

@pikax honestly, I am torn a bit about giving the last example in the docs as we create "two ways of declaring emitted events" and the one with props is definitely less readable. I am open to discussion about this one :)

NataliaTepluhina avatar Oct 16 '21 10:10 NataliaTepluhina

I agree, having multiple ways to describe something is not ideal, but I think we should acknowledge and explain how events and props are merged on v3, not recommend it, but at least explain in the advanced section to have it documented, because for example on he last example, the onClick is not always a function it can be an array of functions and the user must handle that.

EDIT: I see this as a being an unexpected behaviour to someone not knowing how the internals works

pikax avatar Oct 16 '21 10:10 pikax