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

查看:21
本文介绍了是否可以从安装在 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);
},

推荐答案

当您在 parent's mounted 生命周期事件中为在一个 child 的 mounted 事件,处理程序将不会捕获子进程发出的事件,因为处理程序是在子进程已经发出事件之后 添加的.基本上事件的循环是这样的.

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. 父母的创造
  2. 孩子的创造
  3. 儿童装
  4. 父母的挂载

显然还有其他生命周期事件发生,但在这种情况下,这是重要的顺序.

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

如果要处理子进程中发出的事件,则需要在子进程的 mounted 事件之前创建处理程序(调用 $on).

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天全站免登陆