vuejs2 - Vue JS 组件监听孙组件的事件

标签 vuejs2

Vue 文档 ( https://v2.vuejs.org/v2/guide/components.html#Listening-to-Child-Components-Events ) 中有注释,但这也适用于孙子吗?

孙子.vue

   <select class="form-control" v-model="selected" @change="updateValue()" >
     ...
    </select>
  methods: {
    updateValue: function () {
      this.$emit('refreshModel', this.selected)
      this.$emit('input', this.selected)
      console.log('selected ' + this.selected + ' in ' + this.$props.field)
    }
  }

祖 parent .vue

<parent-form ...
              v-on:refresh-model="refreshModel"
             ...
</parent-form>

methods: {
  refreshModel: function (event) {
    console.log("Hello Vue JS");
  },

显然我已经删除了很多代码,希望只留下了要点。

运行结果是显示了孙子中的日志语句,但不显示refreshModel函数。

有人能看出我做错了什么吗?

问候

最佳答案

从 Vue 2.4 开始,有一个非常简单的方法:

  1. 孙子像以前一样发出信号。

  2. 中间代中,只需添加 v-on="$listeners" 即可将信号上游转发到其自己的父代。示例:

    <!-- Relays the signal from its child component to its own parent -->
    <vue-grandchild-component v-on="$listeners">  
    </vue-grandchild-component>
    
  3. 祖 parent (最终信号目的地)中,像以前一样捕获信号。

有关更多详细信息,请包含您可以运行的代码片段,请参阅 this thread

关于vuejs2 - Vue JS 组件监听孙组件的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54888097/

相关文章:

javascript - 如何检查特定元素的 DOM 并创建新的 Vue 实例?

vue.js - 在vue模板中获取未定义的 "Cannot read property ' key'

javascript - Vue.js : Laravel Mix not combining files

javascript - Vuejs - 如何将 Prop 从数据中的数组传递给子组件?

javascript - 如何避免检查对象中的对象是否存在于数据Vue.js中?

vue.js - 如何从vue功能组件提供传递注入(inject)

css - 在vue.js组件中,如何在css中使用props?

javascript - 使用 VueX Store 中的计算属性进行 Ajax 调用的正确方法是什么

vue.js - 如何在 v-select 中为 vue-dropdown 显示名称?

javascript - [Vue warn] : Invalid prop: type check failed for prop "xxx". 预期值为 X 的数字,得到值为 "X"的字符串