Ionic3 发布/订阅
https://ionicframework.com/docs/api/util/Events/
发布:
import { Events } from 'ionic-angular'; //导入
...
export class EventsPage {
user = '来自Events的数据'
constructor(public events: Events) {}
publishEvents(user) {
console.log('User created!')
this.events.publish('user:created',this.user, Date.now());
console.log(this.user)
}
}
订阅
import { Component} from '@angular/core';
import { Events } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
myEvent;
constructor(public events: Events) {
events.subscribe('user:created', (user, time) => {
HomePage.prototype.myEvent = user;
});
}
}