Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在Ionic 5中动态更改侧菜单项_Angular_Ionic Framework_Ionic5 - Fatal编程技术网

Angular 在Ionic 5中动态更改侧菜单项

Angular 在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

我正在将一个项目从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上,我写了以下内容:

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
    
    });
}