vuejs / babel-plugin-transform-vue-jsx

babel plugin for vue 2.0 jsx
1.85k stars 132 forks source link

`on` doesn't work for Lifecycle Hooks #166

Open ArmorDarks opened 5 years ago

ArmorDarks commented 5 years ago

Example

<MyComponent onCreatedHook={() => console.log('MyComponent created')} />

Expected

On creation to log

MyComponent created

Actual

Nothing happens


Looks like Lifecycle Hooks not available through jsx. I've tried onCreated, onHookCreated, onCreatedHook, hookCreate and so on — nothing works.

nickmessing commented 5 years ago

@ArmorDarks, can you provide an example on the alternative in vue template?

ArmorDarks commented 5 years ago

In regular Vue templates it would be

<template>
  <HelloWorld @hook:created="log" />
</template>

<script>
import MyComponent from "./components/MyComponent.vue";

export default {
  name: "App",
  components: { MyComponent },
  methods: {
    log () { console.log('hi from created') }
  }
};
</script>

https://codesandbox.io/s/p7v4zz36yx

nickmessing commented 5 years ago

hmm.. here's a way to do that, I will think about some syntax for it.

For now you can use spread to achieve the same result as shown here

ArmorDarks commented 5 years ago

onCreatedHook seems the most legit, but confusing that it's reversed hook:created. Though, hook: here looks like a namespace.

Another one: hookOnCreated to namespace hook-events

nickmessing commented 5 years ago

I see a problem bigger, there's no reason for people not to use namespaced events in the first place, so we should probably support the full namespaced version like <HelloWorld onHook:created={this.log} />

ArmorDarks commented 5 years ago

Not sure it worth it. onHookCreated sounds not bad too and follows the generic idea of making all standard Vue events into just on versions.

nickmessing commented 5 years ago

@ArmorDarks, how does transpiler know if it's supposed to be hookCreated or hook:created? And JSX supports namespaced arguments

ArmorDarks commented 5 years ago

Same way as it knows that onSomething should be @something and not v-bind:onSomething (which, by the way, has same not that fun implications when you're accidentally naming prop for accepting a callback as onSomething and wonder why it doesn't work).

It just assumes that when you say onHook... you want hook:...

nickmessing commented 5 years ago

@ArmorDarks, yeah, that's done by parsing the on prefix. But nobody stops user from registering event called alpha:beta, how could transform know that onAlphaBeta it should listen for alpha:beta and not alphaBeta? TLDR; JSX allows onAlpha:beta and that's what I will go forward with.

ArmorDarks commented 5 years ago

alpha:beta

I thought hook: is the only namespace. Then it might make sense.

But following the logic of other JSX events, should it be <Comp hook:onCreated={this.log} />?

baby2011 commented 4 years ago

Have you solved your problem? I meet the same problem