0
点赞
收藏
分享

微信扫一扫

Ionic3 发布/订阅

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;
});
}
}

方法

publish(topic, eventData)

发布主题

参数

类型

说明

topic

string

发布的主题

eventData

any

数据

subscribe(topic, handler)

订阅数据

参数

类型

说明

topic

string

the topic to subscribe to

handler

function

the event handler

unsubscribe(topic, handler)

取消订阅

参数

类型

说明

topic

string

the topic to unsubscribe from

handler

function

the event handler

返回值:如果被移除成功,返回true


举报

相关推荐

0 条评论