是否可以从安装在 Vuejs 中发射? [英] Is it possible to emit from mounted in Vuejs?

查看:20
本文介绍了是否可以从安装在 Vuejs 中发射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am familiar with emitting data via bus on events and these work great but now I want to emit something but I don't have an event to tie it to.

I've tried to emit it on mounted but that hasn't worked as below:

mounted(){
  bus.$emit('send-test', this.test);
},

解决方案

When you add an event handler in the parent's mounted lifecycle event for an event that is emitted in a child's mounted event, the handler will not catch the event emitted by the child because the handler is added after the child has already emitted the event. Basically the cycle of events is like this.

  1. Parent's create
  2. Child's create
  3. Child's mounted
  4. Parent's mounted

Obviously there are other lifecycle events that occur, but that's the sequence that matters in this case.

If you want to handle an event emitted in the child, you need to create the handler (call $on) before the child's mounted event.

这篇关于是否可以从安装在 Vuejs 中发射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆