Ionic 3 Events
2018-04-03 本文已影响2人
与蟒唯舞
Events 是一个发布-订阅式事件系统,用于在应用程序中发送和响应应用程序级别的事件。允许您通过简单地发布包含一些数据的事件并在组件中订阅该事件来达到跨多个组件进行通信的目的。
import { Events } from 'ionic-angular';
// first page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
console.log('User created!')
this.events.publish('user:created', user, Date.now());
}
// second page (listen for the user created event after function is called)
constructor(public 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);
});
}