[英]How to use BehaviorSubject in nx module federation?
I have this module federation workspace我有这个模块联合工作区
apps
-- host
---- src
------ app
-------- app.component.ts
-- main
---- src
------ app
-------- app.component.ts
libs
-- components
---- menu
------ src
-------- lib
---------- menu.component.ts
-- services
---- src
------ lib
-------- global.service.ts
global.service.ts全局服务.ts
items$ = new BehaviorSubject<any[]>([]);
setMenuItems(items: any[]): void {
this.items$.next(buttons);
}
menu.component.ts菜单.component.ts
items: any[];
ngOnInit(): void {
this.globalService.items$.subscribe((result) => {
this.items = result;
});
}
host app - app.component.ts主机应用程序 - app.component.ts
ngOnInit(): void {
this.globalService.setMenuItems([1, 2, 3]); // this works
}
main app - app.component.ts主应用程序 - app.component.ts
ngOnInit(): void {
this.globalService.setMenuItems([1, 2, 3]); // this not works
}
I can't use global service in my main app.我无法在我的主应用程序中使用全球服务。
This is the command I use to run the project: nx serve host--devRemotes="main"
这是我用来运行项目的命令: nx serve host--devRemotes="main"
That might be because you're not coding reactively.那可能是因为您没有进行被动编码。
Try this instead.试试这个。
@Component(...)
export class MenuComponent {
items$ = this.globalService.items$.asObservable();
constructor(private globalService: GlobalService) {
}
<div class="menu">
<div *ngFor="let item of items$ | async" class="menu-item">{{ item }}</div>
</div>
Do you use injectable decorator in service class declaration with provideIn: 'root'?您是否在带有 provideIn: 'root' 的服务类声明中使用可注入装饰器?
@Injectable({
providedIn: 'root'
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.