官网文档地址:
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;
});
}
}
发布主题
参数 | 类型 | 说明 |
---|---|---|
topic | string | 发布的主题 |
eventData | any | 数据 |
订阅数据
参数 | 类型 | 说明 |
---|---|---|
topic | string | the topic to subscribe to |
handler | function | the event handler |
取消订阅
参数 | 类型 | 说明 |
---|---|---|
topic | string | the topic to unsubscribe from |
handler | function | the event handler |
返回值:如果被移除成功,返回true