在呈现Angular9中的动态组件时,如何在Domportaloutlet中重复使用组件?
在我的角度应用中,我必须根据不同情况渲染动态组件。因此,每当将新组件附加到 domportalutlet 实例上时,其工作正常。对于不同的情况,将呈现不同的组件。这也有效。 但是,如果再次到达相同的情况,我必须在同一门户实例中重复使用该组件的先前状态的相同组件。所有组件的目标元素相同。因此,需要在同一目标元素中渲染组件。
在html中,
<div #target></div>
在组件 .ts 文件中,
@ViewChild('target', {static: true}) targetElement: ElementRef;
此targetElement
传递给处理动态组件渲染的服务。
在服务中,
private portalOutlet: DomPortalOutlet;
// in constructor,
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private injector: Injector,
private appRef: ApplicationRef,
) { }
public load(container: Element) {
this.portalOutlet = new DomPortalOutlet(
container, this.componentFactoryResolver, this.appRef, this.injector);
const portal = new ComponentPortal(this.getComponent());
this.portalOutlet.attachComponentPortal(portal);
}
public getComponent() {
// returns component based on situation
}
// also handled detaching DomPortalOutlet instance on main component destroy.
// ... other stuffs
// ...
此load
方法是从主组件中调用的,其中this.targetelement.nativeElement
传递。
例如,假设sitation a
,dynamiccomponenta
是渲染的。 dynamicComponenta
的状态和视图将被更改。现在,sitation b
发生,因此在这种情况下,dynamiccomponentb
在相同的 domportaloutlet 实例中渲染。到目前为止没问题。现在,如果站点A
再次发生,则我必须呈现dynamicComponenta
具有与之前呈现时相同的状态和视图。如果再次加载dynamiccomponenta
,我想坚持状态和视图。
我可以实现这个吗?如果是,那么该怎么做?还是其他方法可以实现这一目标?
角版:〜9
In my angular application, I have to render dynamic components based on different situations. So whenever a new component is attached to a DomPortalOutlet instance, its working fine. For different situations different components will be rendered. That is also working.
But if the same situation arrives again, I have to reuse the same component having the previous state of that component in the same portal instance. Where the target element is the same for all components. So need to render component in the same target element.
In Html,
<div #target></div>
In component .ts file,
@ViewChild('target', {static: true}) targetElement: ElementRef;
This targetElement
is passed to a service where dynamic component rendering is handled.
In service,
private portalOutlet: DomPortalOutlet;
// in constructor,
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private injector: Injector,
private appRef: ApplicationRef,
) { }
public load(container: Element) {
this.portalOutlet = new DomPortalOutlet(
container, this.componentFactoryResolver, this.appRef, this.injector);
const portal = new ComponentPortal(this.getComponent());
this.portalOutlet.attachComponentPortal(portal);
}
public getComponent() {
// returns component based on situation
}
// also handled detaching DomPortalOutlet instance on main component destroy.
// ... other stuffs
// ...
This load
method is called from the main component, where this.targetElement.nativeElement
is passed.
For example, let's say for Sitation A
, DynamicComponentA
is rendered. And state and view of DynamicComponentA
will be changed. Now, Sitation B
occurs, so in that case DynamicComponentB
is rendered in the same DomPortalOutlet instance. So far no problem. Now if Sitation A
occurs again, then I have to render DynamicComponentA
with the same state and view as before when it was rendered previously. I want to persist the state and view of DynamicComponentA
if it is loaded again.
Can I achieve this? If yes, then how to do it? Or any other approach to achieve this?
Angular Version: ~9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论