VueJS 2 $emit and $on



我使用vuejs 2,我真的不知道如何从子部件到父母。

我有2个组件: dashboad dashboardpanel

in dashboardpanel ,我有一种方法:

execute () {
    // emit to parent
    this.$emit('executeSQL', this.value)
    ...
}

dashboard

mounted () {
   // get event from DashboardPanel
   this.$on('executeSQL', function(value) {
        alert(value)
   })
}

什么也没发生,我在doc中找不到 $ oon > $,我不知道我是否可以使用其他方法实现它?

您必须在 Dashboard组件中指定如何从 DashboardPanel组件中对 executeSQL事件做出反应。在Dashboard的HTML模板中:

<DashboardPanel v-on:executeSQL="doExecuteSQL($event)" />

doExecuteSQL beeing Dashboard的方法:

methods: {
  doExecuteSQL(value) { ... }
}

希望这会有所帮助。

最新更新