1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nuxt vue-composition-api tsx でemitでケバブケースにするとハマる

Posted at

tsxでのemitについての注意事項

ケバブケースでemitを書いた場合、親componentで受け取ることができなくなるので注意
emit('child-clicked')

上記の場合では動作せず
<child-component onChildClicked={exampleFunction}/>

キャメルケースなどで対応すると動作する
emit('childClicked')

環境

"@vue/composition-api": "^1.0.0-beta.9"
"vue-tsx-support": "^2.3.3"
"@vue/babel-sugar-v-model": "^1.1.2"
"babel-preset-vca-jsx": "^0.3.6"

nuxt.config.ts
~~
  build: {
    babel: {
      plugins: ['module:@vue/babel-sugar-v-model'],
      presets: ['@nuxt/babel-preset-app', 'vca-jsx'],
    },
  }
~~

sample

child.tsx
import { defineComponent } from '@vue/composition-api';
export default defineComponent({
  setup: (_, {emit}) => {
    const onClick = ()=>{
      emit('fooBar', 'Some messages');
      
      // ! emit('foo-bar', 'Some messages') だと動作せずハマる
    }
    return () => (
      <div onClick={onClick} >Child Component!</div>
    );
  },
});
parent.tsx
import { defineComponent } from '@vue/composition-api';
import ChildComponent from '@/components/example/emit';
export default defineComponent({
  setup: () => {
    const handleEmit = (message: string )=>{
      console.log(message);
    }
    return () => (
      <ChildComponent onFooBar={handleEmit} />
    );
  },

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?