RC5:在不同的路由器插座中延迟加载 NgModule [英] RC5 : Lazy loading of NgModule in different router-outlet

查看:23
本文介绍了RC5:在不同的路由器插座中延迟加载 NgModule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RC5 的 NgModule 进行动态路由加载.

I'm using RC5's NgModule to do dynamic route loading.

我的应用有两个深度级别.我有这样的路线:

My app has two depth level. I have routes like :

  • 应用程序/登录
  • 应用程序/仪表板/模块 1
  • 应用程序/仪表板/模块 2
  • 等等……

每个 deph 级别都有自己的路由器插座,因此我可以在每个级别定义自定义布局.即登录名和仪表板显示在应用路由器出口中,而模块 1 和模块 2 显示在仪表板路由器出口中.

Each deph level has it's own router-outlet so I can define custom layout at each level. i.e. login and dashboard are displayed in app router-outlet while module1 and module2 are displayed in dashboard router-outlet.

按需动态加载每条路由的配置是什么?

What is the configuration to load dynamically each route on demand ?

推荐答案

这是一个通过 NgModules 和 router-outlet 动态加载的最小工作示例.

Here is a minimal working example of dynamic loading accoss NgModules and router-outlet.

app.module.ts

@NgModule({
    declarations: [AppComponent],
    imports: [
        RouterModule,
        routing],
    bootstrap: [AppComponent],
    providers: [
        // some providers
    ]
})

export class AppModule { }

app.component.ts

@Component({
  template: '<router-outlet></router-outlet>'
})
export class BadAppComponent { }

/login/dashboard 将在其中布置.

The <router-outlet> where /login and /dashboard are going to be laid out.

app.routes.ts

export const routes: Routes = [
    {path: '', redirectTo: '/login', terminal: true},
    {path: 'login', component: LoginComponent},
    {path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module#DashboardModule'}
];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

loadChildren 指向要按需加载的模块.

loadChildren point to the module to be loaded on demand.

dashboard.module.ts

@NgModule({
    declarations: [
        DashboardComponent
    ],
    imports: [CommonModule, RouterModule, routing],
    exports: [DashboardComponent],
    providers: [
        // some providers
    ]
})
export class DashboardModule { }

dashboard.component.ts

@Component({
  moduleId: module.id,
  template: '<div>sidebar left</div><router-outlet></router-outlet><div>sidebar right</div>',
})
export class DashboardComponent {
  constructor() {}
}

其中 /dashboard/accounts/dashboard/transfert 将被布局.你不应该命名路由器插座

<router-outlet> where /dashboard/accounts and /dashboard/transfert are going to be laid-out. You should not name the router-outlet

dashboard.routes.ts

export const routing: ModuleWithProviders = RouterModule.forChild([
    {path: '', component: DashboardComponent,
        children: [
            { path: 'accounts', loadChildren: 'app/dashboard/accounts/accounts.module#DashboardAccountsModule'},
            { path: 'virement', loadChildren: 'app/dashboard/transfert/transfert.module#DashboardTransfertModule'}
        ]
    }

]);

children 确保子路由加载到当前 即仪表板的 router-outler

children ensures that children routes are loaded in current <router-outlet> i.e. dashboard's router-outler

accounts.module.ts

@NgModule({
    declarations: [
        AccountsFragment
    ],
    imports: [CommonModule, RouterModule, routing],
    exports: [AccountsFragment]
})
export class DashboardAccountsModule { }

accounts.routing.ts

export const routing: ModuleWithProviders = RouterModule.forChild([
    { path: '', component: AccountsComponent}
]);

这是路线的终点.它将显示在仪表板的路由器出口中,因为它是仪表板的子路由.

This is the end of the route. It will be displayed in the dashboard's router-outlet because is is a children route of dashboard.

accounts.component.ts

@Component({
    moduleId: module.id,
    template: '<div>Your accounts!!</div>'
})
export class AccountsComponents {
}

就是这样.您应该拥有构建随时加载"应用程序所需的一切.希望有帮助.

That is it. You should have all you need to structure your 'load as you go' application. Hope it helps.

这篇关于RC5:在不同的路由器插座中延迟加载 NgModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆