Event监听器

记录一个angular里很有用的干货

假设side bar写在了项目app.component.html里
(请不要把side bar写在app.component.html里)

那么app.component.html是只会init一次的
这时候要更新app.component.html的信息的话

可以添加一个event监听器
这里监听器我选择添加在service的callback里

发布事件

//user.service.ts
this.httpclient.get().subscribe(data=>{
console.log();
this.eventCtrl.publish("user:login", data.name, data, email)
})

监听

//app.component.html

import { Events} from '@ionic/angular';

constructor(public eventCtrl: Events){
}

initializeApp(){
    this.eventCtrl.subscribe('user:login', (name, email) => {
        this.name = name;
        this.email = email;
    });
}
//subscribe(str: description, agrv: params)

你可能感兴趣的:(Event监听器)