简体   繁体   English

Angular 10个动态添加的元件断片

[英]Angular 10 Dynamically Added Components Breaking Tabs

I have been tasked with overhauling the main page of our collection of web applications.我的任务是彻底检查我们的 web 应用程序集合的主页。

Currently we have inherited some other applications whose menu systems were bulky and pushed well past what we wanted for our user experience in terms of clicks and navigation.目前,我们已经继承了一些其他应用程序,它们的菜单系统非常庞大,并且在点击和导航方面远远超出了我们想要的用户体验。

I volunteered to do my best to turn our little circus into a SPA.我自愿尽我所能把我们的小马戏团变成一个SPA。 I figured what I could do is load manage it so there was no more traditional navigation between apps and just dynamically load the apps right into the user's workspace.我想我能做的就是对其进行加载管理,这样应用程序之间就不再需要传统的导航,只需将应用程序动态加载到用户的工作区中即可。

For the most part this seems good;在大多数情况下,这似乎很好。 it's not perfect but works.它并不完美,但有效。

Then I noticed: we have a couple applications whose parent component is a component containing tabs.然后我注意到:我们有几个应用程序,其父组件是包含选项卡的组件。 And the dynamic loader is not loading those tabs.并且动态加载器不会加载这些选项卡。 In fact it is breaking the material tabs in almost any loaded component.事实上,它几乎破坏了所有已加载组件中的材料标签。

Loading Function:加载 Function:

injectDynamicComponent(componentName) {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentName);
        const containerRef = this.viewContainerRef;
        containerRef.clear();
        containerRef.createComponent(componentFactory);
       } 

Declaration And Constructor:声明和构造函数:

    @ViewChild('dynamicInsert', { read: ViewContainerRef }) dynamicInsert: ViewContainerRef;
    @ViewChild('navGroup') navGroup: ElementRef;

    constructor(
        private uiService: UserInfoService,
        private phService: PageHeaderService,
        private aduserService: ADUserService,
        private menuService: MenuService,
        private viewContainerRef: ViewContainerRef,
        private componentFactoryResolver: ComponentFactoryResolver,
        private router: Router,
        private configService: ConfigsLoaderService,
        private renderer: Renderer2) {)

HTML for Injected Component: HTML 用于注入组件:

        <div style="min-height:100%; max-width: 90% !important;">
          <div #dynamicInsert></div>
        </div>

Injected Component for Reference:注入组件供参考:

<div fxLayout="column" fxLayoutAlign="center" style="max-width: 90% !important;">
  <mat-tab-group mat-align-tabs="center">
    <mat-tab label="Call Tracker" *ngIf="isAgent || isLead || isSuper"><app-calltracker></app-calltracker></mat-tab>
    <mat-tab label="Call Report" *ngIf="isLead || isSuper"><app-callreport></app-callreport></mat-tab>
    <mat-tab label="Call Types" *ngIf="isSuper"><app-calltypes></app-calltypes></mat-tab>
  </mat-tab-group>
</div>

没有标签

Above you can see no tabs loaded.在上面你可以看到没有加载任何标签。 Below you can see the tab loading.您可以在下面看到标签加载。 I am also currently in the process of changing the input types which is why they look different.我目前也在更改输入类型,这就是它们看起来不同的原因。

存在的标签

Try calling this.componentRef.changeDetectorRef.detectChanges();尝试调用this.componentRef.changeDetectorRef.detectChanges(); after the component creation.组件创建后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM