Angular 在Ionic 5中动态更改侧菜单项
我正在将一个项目从ionic 3.2迁移到5.14,但我在使用可观察对象而不是事件时遇到了问题 在原始代码中,用户登录后,我通过事件更改了侧菜单的名称和图像: 应用程序组件.tsAngular 在Ionic 5中动态更改侧菜单项,angular,ionic-framework,ionic5,Angular,Ionic Framework,Ionic5,我正在将一个项目从ionic 3.2迁移到5.14,但我在使用可观察对象而不是事件时遇到了问题 在原始代码中,用户登录后,我通过事件更改了侧菜单的名称和图像: 应用程序组件.ts this.events.publish('user:login', this.nomePrimeiro, Date.now()); this.events.publish('image:login', this.imagem, Date.now()); 在app.component.ts上,我写了以下内容: even
this.events.publish('user:login', this.nomePrimeiro, Date.now());
this.events.publish('image:login', this.imagem, Date.now());
在app.component.ts上,我写了以下内容:
events.subscribe('user:login', (user, time) => {
Global.nomePrimeiro = user;
});
events.subscribe('image:login', (image, time) => {
Global.imagem = image;
});
如何为可观察对象更改此值?
我需要服务?创建事件服务。
在EventService.ts中:
export class EventService {
private dataObserved = new BehaviorSubject<any>('');
currentEvent = this.dataObserved.asObservable();
constructo(){}
publish(param):void {
this.dataObserved.next(param);
}
}
第2页:
constructor(public eventService:EventService){
this.eventService.currentEvent.subscribe(data=>{
// here you can get the data or do whatever you want or data.name or data.value
});
}
非常感谢莫斯塔法。这就是我所需要的,一个关于如何使用可观测数据的简单而清晰的例子。
constructor(public eventService:EventService){
this.eventService.currentEvent.subscribe(data=>{
// here you can get the data or do whatever you want or data.name or data.value
});
}