Vue.js 在一个Vue组件中绑定一个类';从另一个Vue组件发出的窗口事件的

Vue.js 在一个Vue组件中绑定一个类';从另一个Vue组件发出的窗口事件的,vue.js,Vue.js,为了能够在所有实例中发出事件并侦听这些事件,我执行以下操作: window.Event = new Vue(); 然后在Vue组件的模板中,我发出事件“isSelected”,如下所示 Vue.component('artikel',{ template: '<li style="list-style-type:none;"><input @click="isSelected" type="checkbox" class="kryssruta" /><sl

为了能够在所有实例中发出事件并侦听这些事件,我执行以下操作:

window.Event = new Vue();
然后在Vue组件的模板中,我发出事件“isSelected”,如下所示

Vue.component('artikel',{
    template: '<li style="list-style-type:none;"><input @click="isSelected" type="checkbox" class="kryssruta" /><slot></slot></li>',

    methods:{
      isSelected(){
        Event.$emit('isSelected');
      }
    }

});
但据我所知,IE不支持箭头函数,所以我也想知道如何使用Joshua Minkler提出的关于.bind(这个)的建议,但我无法让它发挥作用。。。我得到“切换未定义”


由于额外的功能,
存在范围问题。如果您在事件侦听器中使用箭头函数,它应该可以正常工作。您还可以通过调用函数来解决范围问题。这看起来像是事件总线的一个用例:Shirtle:谢谢,这很有效!约书亚·明克勒:你能看看我最新的问题吗?我不明白。把(这个)绑起来工作。。。我误解你了吗?
Vue.component('modal', {
  template: `
          <button class="button" :class="{grey:toggle}">Lägg till</button>
  `,

  data(){
    return{
      toggle: true
    }
  },

  created(){
    Event.$on('isSelected', function() {
      alert('works!');
      this.toggle = false;
    })
  },
  created(){
Event.$on('isSelected', () =>  this.toggle=!this.toggle);
  },
  created(){
    Event.$on('isSelected', function() {
      toggle.bind(this) = true;
    })