events - 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。
我究竟做错了什么。
解决方案
将您的事件订阅代码放在另一个页面的构造函数中。
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);
});
}
推荐阅读
- c# - 在 Xamarin iOS 中打开 url 后是否应该处理 NsUrl?
- mapbox - 使用 Mapbox SDK 6.7 启用 pro Guard 时无法生成构建
- c++ - 静态声明时的c ++复制构造错误
- javascript - 如何使用 socket.io 接收数据?
- .net - Microsoft Bot Framework OAuth 卡返回错误请求
- javascript - 如何创建 Konva-React 上下文菜单
- c++ - GNU 链接器:适应名称修改算法的更改
- java - Java - 下拉组件显示
- tensorflow - Theano / Pytorch / Tensorflow 可以自动计算以下梯度吗?
- python - 稀疏张量上的行或元素选择