Search code examples
angulartypescriptlazy-loadingtabviewprimeng

How to lazy load Angular 2 components in a TabView (PrimeNG)?


It is my app.component.ts:

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

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

}

And this is my 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>

My three components (home, tree and config) are been loaded at the same time when the tabView is loaded. However, I would like that a component was loaded just when its tab was selected. How to do that?

P.S.: if it helps, TabView has an onChange event.


Solution

  • After much research, I could solve the problem using router. Now the application is really fast.

    app.component.ts:

    import { Component } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
        templateUrl: 'app/app.component.html',
        selector: 'my-app'
    })
    export class AppComponent {
    
        constructor(
            private router: Router) {
        }
    
        handleChange(e) {
            let index = e.index;
            let link;
            switch (index) {
                case 0:
                    link = ['/home'];
                    break;
                case 1:
                    link = ['/hierarquia'];
                    break;
                case 2:
                    link = ['/config'];
                    break;
            }
            this.router.navigate(link);
        }
    }
    

    app.component.html:

    <div>
        <p-tabView (onChange)="handleChange($event)">
            <p-tabPanel header="Home" leftIcon="fa-bar-chart-o"></p-tabPanel>
            <p-tabPanel header="Hierarquia" leftIcon="fa-sitemap"></p-tabPanel>
            <p-tabPanel header="Configurações" leftIcon="fa-cog"></p-tabPanel>
        </p-tabView>
    </div>
    
    <router-outlet></router-outlet>
    

    app.route.ts:

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { AppHome } from './app.home';
    import { AppTree } from './app.tree';
    import { AppConfig } from './app.config';
    
    const routes: Routes = [
        {
            path: 'home',
            component: AppHome
        },
        {
            path: 'hierarquia',
            component: AppTree
        },
        {
            path: 'config',
            component: AppConfig
        },
        {
            path: '',
            redirectTo: '/home',
            pathMatch: 'full'
        },
    ];
    
    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
    export const routedComponents = [AppHome, AppTree, AppConfig];
    

    app.module.ts:

    import { NgModule } from '@angular/core';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    import { HttpModule } from '@angular/http';
    import { BrowserModule } from '@angular/platform-browser';
    import 'rxjs/add/operator/toPromise';
    
    import { AppConfig } from './app.config';
    import { AppHeader } from './app.header';
    import { AppHome } from './app.home';
    import { AppTree } from './app.tree';
    import { AppComponent } from './app.component';
    
    import { AppRoutingModule, routedComponents } from './app.route';
    
    import { InputTextModule, DataTableModule, ButtonModule, DialogModule, TabViewModule, ChartModule, TreeModule, GrowlModule, InputSwitchModule, BlockUIModule, InputMaskModule, DropdownModule } from 'primeng/primeng';
    
    @NgModule({
        imports: [BrowserModule, FormsModule, ReactiveFormsModule, HttpModule, AppRoutingModule, InputTextModule, DataTableModule, ButtonModule, DialogModule, TabViewModule, ChartModule, TreeModule, GrowlModule, InputSwitchModule, BlockUIModule, InputMaskModule, DropdownModule],
        declarations: [AppHeader, AppComponent, AppHome, AppTree, AppConfig, routedComponents],
        bootstrap: [AppHeader, AppComponent]
    })
    export class AppModule { }
    

    Thanks God! =]