Events
事件是一种发布订阅风格的事件系统,用于在整个应用程序中发送和响应应用程序级事件。
使用
import { Events } from 'ionic-angular';
constructor(public events: Events) {}
// first page (publish an event when a user is created)
function createUser(user) {
console.log('User created!')
events.publish('user:created', user, Date.now());
}
// second page (listen for the user created event)
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);
});
效果:
实例对象
publish(topic, eventData)
将事件发布到给定的主题。
Param | Type | Details |
---|---|---|
topic | string |
the topic to publish to |
eventData | any |
the data to send as the event |
subscribe(topic, handler)
订阅活动主题。 发布到该主题的事件将触发提供的处理程序。
Param | Type | Details |
---|---|---|
topic | string |
the topic to subscribe to |
handler | function |
the event handler |
unsubscribe(topic, handler)
取消订阅给定的主题。 您的处理程序将不再接收发布到此主题的事件。
Param | Type | Details |
---|---|---|
topic | string |
the topic to unsubscribe from |
handler | function |
the event handler |
Returns:
如果处理程序被移除就返回 true