【发布时间】:2021-07-04 05:04:10
【问题描述】:
我使用Angular portal动态渲染组件。
组件在订阅中监听新的portal$组件:
ngOnInit() {
this.portal$ = this.mapLibraryService.portalBridgeService.portal$;
this.portal$.subscribe((e) => console.log(e));
}
那么模板有:
<ng-template [cdkPortalOutlet]="portal$ | async"></ng-template>
在控制台日志中,我得到了我尝试渲染的组件:
但它不会呈现,直到我不点击任何地方或调整窗口大小或在页面上执行任何操作。
如何调试和修复?
那是我改变portal$主题的地方:
editLayer(parameters: SearchParameters): Promise<any> {
return this.showObjects(parameters).then((response) => {
this.portalBridgeService.set(this.portalBridgeService.portals.EDIT);
});
}
这是 this.portalBridgeService.set(this.portalBridgeService.portals.EDIT); 我如何将数据发送到portal$.
如果要搬家:
this.portalBridgeService.set(this.portalBridgeService.portals.EDIT);upper from promise 它工作正常。
重要事项:
如果要在订阅中进行此操作:
this.portal$ = this.mapLibraryService.portalBridgeService.portal$;
this.portal$.subscribe((e) => {
setTimeout(() => cdr.detectChanges())
});
它开始工作,它呈现组件,到那时,如果发生变化,就不会再出现了。
我尝试了另一种方式,在根组件中我有:
export class AppComponent implements OnInit, OnDestroy {
public action: any;
ngOnInit() {
this.mapLibrary.portalBridgeService.portal$.subscribe((e) => {
if (e.type === 'Map') {
this.action = EditMapWrapperComponentComponent;
}
});
}
}
模板是:
<ng-container *ngComponentOutlet="action"></ng-container>
服务:
export class PortalBridgeService {
private portalSubject = new ReplaySubject<Portal>();
public portal$ = this.portalSubject.asObservable();
public set(portal: Portal) {
this.portalSubject.next(portal);
}
}
我有同样的问题,我收到消息,但 Angular 不会在组件 AppComponent 中呈现它,直到我调整页面大小或随时单击。
【问题讨论】:
-
你使用什么变化检测策略?
-
默认更改检测。不推
标签: angular