Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Events Ionic3事件(发布)不工作_Events_Ionic Framework_Ionic3 - Fatal编程技术网

Events Ionic3事件(发布)不工作

Events Ionic3事件(发布)不工作,events,ionic-framework,ionic3,Events,Ionic Framework,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

我有密码

在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);
});
在另一页我正在做

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);
});
但是在事件中没有发生任何事情。订阅。它不工作,没有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);
    });
}
constructor(private events: Events) {
    this._events.subscribe('user:created', (user,time) => {
      console.log('Welcome', user, 'at', time);
    });
}

将事件订阅代码放入另一页的构造函数中

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);
    });
}
constructor(private events: Events) {
    this._events.subscribe('user:created', (user,time) => {
      console.log('Welcome', user, 'at', time);
    });
}

听起来你想在两页纸之间交流?根据我的经验,除非有中间人,否则使用离子事件在两个页面之间进行通信不是很有用。如果一个页面触发一个事件,那么另一个页面很可能不会被实例化以听到该事件


页面当然可以发布事件。在服务/提供商或app.component.ts中,订阅可能会更成功。然后,他们可以响应已发布的事件,并导航到另一个页面或公开由事件操纵的属性,这些页面可以根据需要使用这些属性。

听起来好像您正在尝试在两个页面之间进行通信?根据我的经验,除非有中间人,否则使用离子事件在两个页面之间进行通信不是很有用。如果一个页面触发一个事件,那么另一个页面很可能不会被实例化以听到该事件


页面当然可以发布事件。在服务/提供商或app.component.ts中,订阅可能会更成功。然后,他们可以响应已发布的事件,并导航到另一个页面或公开由事件操纵的属性,页面可以根据需要使用这些属性。

您可以在登录后调用events publish方法

组件中声明变量

例:Some:any

以所需的任何方法发布事件

this.\u events.publish('user:created',this.some=“data”,Date.now())

然后您可以在另一个页面的构造函数中订阅事件

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);
    });
}
constructor(private events: Events) {
    this._events.subscribe('user:created', (user,time) => {
      console.log('Welcome', user, 'at', time);
    });
}

您可以在登录后调用events publish方法

组件中声明变量

例:Some:any

以所需的任何方法发布事件

this.\u events.publish('user:created',this.some=“data”,Date.now())

然后您可以在另一个页面的构造函数中订阅事件

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);
    });
}
constructor(private events: Events) {
    this._events.subscribe('user:created', (user,time) => {
      console.log('Welcome', user, 'at', time);
    });
}

其他答案部分正确,但如果要在构造函数中使用subscribe,则必须确保发布者在第二个页面已订阅后发布

当您有订阅者时,它可以工作,您可以使用:

ionViewDidLeave() {
    this.events.publish('queue:name', object);
}

其他答案部分正确,但如果要在构造函数中使用subscribe,则必须确保发布者在第二个页面已订阅后发布

当您有订阅者时,它可以工作,您可以使用:

ionViewDidLeave() {
    this.events.publish('queue:name', object);
}

你能提供更多的上下文吗?也许你在发布活动后才订阅?你能提供更多的上下文吗?也许你在发布活动后就订阅了?