【问题标题】:Vue instance's $on method is not workVue 实例的 $on 方法不起作用
【发布时间】:2018-05-15 03:11:23
【问题描述】:

我刚刚在 main.js 文件中创建了一个事件总线,如下所示:

main.js

Vue.prototype.$bus = new Vue()

在那之后,我只是写了一些代码来测试这样的事件总线:

测试组件

<template>
  <div>
      <div class="account-modal_form">
          <form action="" @submit.prevent="formSubmit">
                <div class="account-modal_form__group" :class="{ warning: errors.has('password') }">
                    <div class="account-modal_form__input">
                        <input name="password" :type="passwordType" placeholder="" class="width-316" v-validate="'required'" v-model="password">
                        <i class="account-modal_form__viewpass" @click="togglePassword"></i>
                    </div>
                    <span class="account-modal_form__warning" v-show="errors.has('password')">
                        {{ errors.first('password') }}
                    </span>
                </div>
                {{ errors }}
                <div class="account-modal_form__group">
                    <button type="submit" class="btn btn--primary btn--large">next</button>
                    <button type="button" class="btn btn--default" @click="cancelAction">cancel</button>
                </div>
          </form>
      </div>
  </div>
</template>

<script>
import { API } from '@/api'

export default {
    data() {
        return {
            passwordType: 'password',
            password: ''
        }
    },
    methods: {
        created() {
            this.$bus.$on('test', () => console.log('test'));
        },
        nextStep() {
            this.$bus.$emit('test');
        },
        formSubmit() {
            this.nextStep();
        }
    }
}
</script>

当我点击提交按钮时,我想先提交表单并调用 nextstep 来发出事件,但 $on 事件什么也没输出。

【问题讨论】:

  • 出现任何错误? Vue.prototype.$bus = ... 是在创建 Vue 实例之前完成的吗?
  • @JacobGoh 不,似乎 sendOriginPassword 事件没有被监听并且什么也没输出。我尝试在发射后输出 this.$bus._event,但它输出为空。

标签: vue.js vuejs2 vuex


【解决方案1】:

您在$on 之前运行$emit,因此当您触发事件时,此时没有侦听器,最好在组件created 生命周期事件上注册您的侦听器,否则无论何时运行你的测试方法你将注册一个新的监听器:

Vue.prototype.$bus = new Vue();

Vue.component('spy-component', {
  template: '<p>{{this.text}}</p>',
  data() {
    return {
      text: '',
    }
  },
  created() {
    this.$bus.$on('sendOriginPassword', (text) => {
      this.text = text;
    });
  }
})

Vue.component('test-component', {
  template: '<button @click="test">Click me</button>',
  created() {
        this.$bus.$on('sendOriginPassword', () => {
      console.log('I am listening event')
    });
  },
  methods: {
    test() {
      this.$bus.$emit('sendOriginPassword', 'Can you hear me?');
    }
  }
});

new Vue({
  el: "#app",
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
  <spy-component></spy-component>
  <test-component></test-component>
</div>

【讨论】:

  • 谢谢,它有效。但是,如果我在另一个组件的创建生命周期中监听测试事件,它也不会输出任何内容,为什么?
  • 它应该可以工作...我修改了示例代码,现在检查一下。
  • 我在问题中粘贴了我的代码,这次测试事件什么也没输出。我不知道为什么。
  • @ChenLee created() {} 应该在 methods 之外
  • 为什么 this.$bus._events 包含测试事件,但是如果我调用 this.$bus.$on('test', () => console.log('test')),它在另一个组件中什么都不输出?
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 2017-03-30
  • 1970-01-01
相关资源
最近更新 更多