首页 > 解决方案 > Ionic3 事件(发布)不起作用

问题描述

我有一个代码

在 login.ts 我正在做

this._events.publish('user:created', 'val', Date.now());

在另一页我正在做

 this._events.subscribe('user:created', (user,time) => {
  // user and time are the same arguments passed in `events.publish(user, time)`
  console.log('Welcome', user, 'at', time);
});

但是_events.subscribe没有发生任何事情。它不工作,没有console.log。

我究竟做错了什么。

标签: eventsionic-frameworkionic3

解决方案


将您的事件订阅代码放在另一个页面的构造函数中。

constructor(private events: Events) {
    events.subscribe('user:created', (user,time) => {
      // user and time are the same arguments passed in `events.publish(user, time)`
      console.log('Welcome', user, 'at', time);
    });
}

推荐阅读