在呈现Angular9中的动态组件时,如何在Domportaloutlet中重复使用组件?

发布于 01-23 23:37 字数 1774 浏览 7 评论 0原文

在我的角度应用中,我必须根据不同情况渲染动态组件。因此,每当将新组件附加到 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 adynamiccomponenta是渲染的。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文