記錄一個(gè)angular里很有用的干貨
假設(shè)side bar寫在了項(xiàng)目app.component.html里
(請(qǐng)不要把side bar寫在app.component.html里)
那么app.component.html是只會(huì)init一次的
這時(shí)候要更新app.component.html的信息的話
可以添加一個(gè)event監(jiān)聽器
這里監(jiān)聽器我選擇添加在service的callback里
發(fā)布事件
//user.service.ts
this.httpclient.get().subscribe(data=>{
console.log();
this.eventCtrl.publish("user:login", data.name, data, email)
})
監(jiān)聽
//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)