首页 > 解决方案 > Vue:使用按钮单击时,发射器在子级中不起作用

问题描述

单击按钮时未调用我的发出调用。我试图运行一个调试器并单步执行它并触发它,但看起来它直接执行并跳过它。我不确定问题是什么或为什么会跳过。

家长

<step-review @emitgetappdata="getAppData"></step-review> 
getAppData: function(data=null) {
  debugger
}

步骤回顾

<button @click="this.clickedButton()">Test</button>
clickedButton: function() {
  console.log("reached here");
  let data = {
    text: "Foo",
  }
  this.$emit('emitgetappdata', data);
}

标签: vue.jsvuejs2

解决方案


您不能this在视图中使用

<button @click="clickedButton()">Test</button>
clickedButton() {
  console.log("reached here");
  let data = {
    text: "Foo",
  }
  this.$emit('emitgetappdata', data);
}

并且您的组件代码对此更加干净。

<step-review @emitgetappdata="getAppData"></step-review> 

getAppData(data=null) {
  debugger
}

阅读一些文档后编辑: https ://vuejs.org/v2/guide/instance.html#Data-and-Methods


推荐阅读