简体   繁体   English

子组件发出自定义事件,但未触发父组件的侦听器

[英]child component emit an custom event, but parent component's listener not triggered

I'm registered a child component in Vue, the child will emit an custom event, but the parent component's listener not triggered.我在 Vue 中注册了一个子组件,子组件会发出一个自定义事件,但没有触发父组件的侦听器。

<template id="add-item-template">
<div class="input-group">
  <input @keyup.enter="addItem" v-model="newItem" >
  <span class="input-group-btn">
    <button @click="addItem" type="button">Add!</button>
  </span>
</div>
</template>

<div id="app" class="container">
  <h2>{{ title }}</h2>
  <add-item-component v-on:itemAdd="addAItem"></add-item-component>
</div>

Vue.component('add-item-component', {
  template: '#add-item-template',
  data: function () {
    return {
        newItem: ''
    };
  },
  methods: {
    addItem() {
      this.$emit('itemAdd');
      console.log("itemAdd In add-item-component");
    }
  }
});

new Vue({
  el: '#app',
  data: { 
    title: 'Welcome to Vue' 
  },
  methods: {
    addAItem: function () {
      console.log("In #app, addAItem()!");
    }
  }
});

The "itemAdd In add-item-component" log show in console, but "In #app, addAItem()!" “itemAdd In add-item-component”日志显示在控制台中,但“在#app中,addAItem()!” log not, the #app's method addAItem not invoked.不要登录,#app 的方法 addAItem 没有被调用。

The problem is that custom events should not be named with camelCase.问题是自定义事件不应以驼峰命名。 If you look at the error message in the console, it tells you:如果您查看控制台中的错误消息,它会告诉您:

Event "itemadd" is emitted in component but the handler is registered for "itemAdd"事件“itemadd”在组件中发出,但处理程序为“itemAdd”注册

The component is emitting a lowercase version even though you've used camelCase to name it.即使您已使用驼峰命名法,该组件仍发出小写版本。 Renaming to all lowercase or kebab-case will fix it.重命名为所有小写或 kebab-case 将修复它。

In the parent template:在父模板中:

<add-item-component @itemadd="addAItem">

Emitting from the child:从孩子发出:

this.$emit('itemadd');

This is discussed a bit with Evan You (Vue creator) here在这里与 Evan You(Vue 创造者)讨论了一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue 3:从父组件向子组件发出事件 - Vue 3: Emit event from parent to child component $ emit从子级到父级Vue 2的事件 - $emit an event from child to parent component Vue 2 Angular 4父组件事件监听器不会被子事件触发 - Angular 4 parent component event listener won't triggered by child emitted event 为什么不更新我在子组件中传递的父组件中的值并更新使用自定义事件 $emit Vue 3? - Why not updating value in the parent component which I passed in child component and updating use custom event $emit Vue 3? 堆叠的父子组件未触发子组件的onClick事件 - onClick event of child component is not getting triggered for stacked parent and child component VueJS如何从子组件向其父组件发出事件 - VueJS How to emit an event from a child component to its parent component 如何设置 Vue 3 父组件向子组件发出事件 - How to set up Vue 3 parent component to emit event to child component 无法从Vue.js的子组件向父组件发出事件 - Not able to emit an event to a parent component from child component in Vuejs VueJS:在子组件中触发事件时,在父组件中运行功能 - VueJS: Run function in parent component when event triggered in child component 父级的Vue.js $ emit事件,并在组件(子级)上接收 - Vue.js $emit event from parent and receive it on a component (child)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM