VueJs v-on:event和this之间有什么区别.$ on(event,handler)?

And*_* Li 6 javascript vue.js vue-events

我正在学习Vuejs event handling.
我认为,开发人员可以使用this.$on('event', handler)js文件来处理'event'.

有一个例子.

<div id="mainapp" v-on:event="processEventFromView">
    <button type="button" v-on:click="emitEvent">
       Emit Event
    </button>
</div>
Run Code Online (Sandbox Code Playgroud)

js文件

var app = new Vue({
  el:"#mainapp",
  data:{
    show:false
  },
  created:function(){
     this.$on('event', this.processEvent);
  },
  methods:{
      emitEvent:function(){
          this.$emit('event', {data:'mydata'});
      },
      processEvent(data){
         console.log('js', data);  //this is fired when clicking the button.
      },
      processEventFromView(data){
         console.log('view', data);  //this is not fired whenever.
      }      

   }
})
Run Code Online (Sandbox Code Playgroud)

但是在示例中,单击按钮时仅触发processEvent附加的处理程序this.$on().v-onvs 之间有什么区别this.$on
为什么v-on:event="processEventFromView"不随时打电话?
我可以附上参考按钮event handlerclick事件ref,而不是v-on:click="emitEvent"
请帮帮我,我错了.

sof*_*ear 1

我猜它与 Vue 的 Linus Berg 相关并在这里回答了/sf/answers/2531178891/ 虽然它与 Vue 的早期版本相关(该帖子来自 2016 年),但我想它仍然适用。

简而言之,回答你的问题

为什么 v-on:event="processEventFromView" 不被调用?

是(我引用)

不能在模板中使用 v-on:custom-event-name (只能在组件上使用)。