gpt4 book ai didi

angular - 如何在 TabView (PrimeNG) 中延迟加载 Angular 2 组件?

转载 作者:太空狗 更新时间:2023-10-29 17:43:30 25 4
gpt4 key购买 nike

这是我的 app.component.ts:

import { Component } from '@angular/core';

@Component({
templateUrl: 'app/app.component.html',
selector: 'my-app'
})
export class AppComponent {

}

这是我的 app.component.html:

<p-tabView>
<p-tabPanel header="Home" leftIcon="fa-bar-chart-o">
<home-app></home-app>
</p-tabPanel>
<p-tabPanel header="Hierarquia" leftIcon="fa-sitemap">
<tree-app></tree-app>
</p-tabPanel>
<p-tabPanel header="Configurações" leftIcon="fa-cog">
<config-app></config-app>
</p-tabPanel>
</p-tabView>

我的三个组件(home、tree和config)是在tabView加载的时候同时加载的。但是,我希望在选择选项卡时加载组件。如何做到这一点?

P.S.:如果有帮助,TabView 有一个 onChange 事件。

最佳答案

你可以使用 SystemJsNgModuleLoader 用于 angular2 路由

Live Plunker

enter image description here

首先,您可以编写将加载模块的组件:

@Component({
selector: 'dynamic-container',
template: `
<template #container></template>
<div *ngIf="!loaded" class="loader"></div>
`,
styles: [`
.loader {
position: relative;
min-height: 100px;
}

.loader:after {
content: 'Loading module. Please waiting...';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
`]
})
export class DynamicContainerComponent implements OnDestroy {
@ViewChild('container', { read: ViewContainerRef }) vcRef: ViewContainerRef;
loaded: boolean;

constructor(private moduleLoader: SystemJsNgModuleLoader) { }

compRef: ComponentRef<any>;

@Input() modulePath: string;
@Input() moduleName: string;

_inited: boolean
set inited(val: boolean) {
if(val) {
this.loadComponent();
}
this._inited = val;
};

get inited() {
return this._inited;
}

loadComponent() {
this.moduleLoader.load(`${this.modulePath}#${this.moduleName}`)
.then((moduleFactory: NgModuleFactory<any>) => {
const vcRef = this.vcRef;
const ngModuleRef = moduleFactory.create(vcRef.parentInjector);
const comp = ngModuleRef.injector.get(LazyLoadConfig).component;
const compFactory = ngModuleRef.componentFactoryResolver.resolveComponentFactory(comp);
this.compRef = vcRef.createComponent(compFactory, 0, ngModuleRef.injector);

this.loaded = true;
});
}

ngOnDestroy() {
this.compRef.destroy();
}
}

然后在你的组件中使用它:

@Component({
selector: 'my-app',
template: `
<h2 class="plunker-title">How to lazy load Angular 2 components in a TabView (PrimeNG)?</h2>
<p-tabView (onChange)="handleChange($event)">
<p-tabPanel header="Home" leftIcon="fa-bar-chart-o">
<home-app></home-app>
</p-tabPanel>
<p-tabPanel header="Hierarquia" leftIcon="fa-sitemap">
<dynamic-container modulePath="./src/modules/tree/tree.module" moduleName="TreeModule"></dynamic-container>
</p-tabPanel>
<p-tabPanel header="Configurações" leftIcon="fa-cog">
<dynamic-container modulePath="./src/modules/config/config.module" moduleName="ConfigModule"></dynamic-container>
</p-tabPanel>
</p-tabView>
`
})
export class AppComponent {
@ViewChildren(DynamicContainerComponent) dynamicContainers: QueryList<DynamicContainerComponent>;

handleChange(e) {
let dynamicContainer = this.dynamicContainers.toArray()[e.index - 1];
if (!dynamicContainer || dynamicContainer.inited) return;

// prevent fast clicking and double loading
dynamicContainer.inited = true;
}
}

另见

关于angular - 如何在 TabView (PrimeNG) 中延迟加载 Angular 2 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com