2016-10-10 151 views
2

我想延迟加载模块与子路由声明在它的路由配置文件, 懒惰模块加载成功,但我不能导航到它的子路由。如何导航到延迟加载的模块子路由?

我正在使用这种技术,就好像它是一个热切加载的模块。 我宣布道路,里面是儿童。 请参阅Plunker中的'moduleB'路由文件 链接按钮正尝试导航到子路由并加载CmpC。但它失败了。

这里是我的根模块路由decalration:

{ path: 'feature-b', loadChildren:'src/featureB/featureB.module#FeatureBModule' , pathMatch: 'full'} 

这是我懒加载模块路由声明。

export const moduleBRoutes: Routes = [ 

    { path: '', component:CmpC , pathMatch: 'full',children:[ 
     {path: '' , pathMatch: 'full'}, 
     { path: 'c', component:CmpB , pathMatch: 'full'} 
    ]}, 

]; 

export const subRouting: ModuleWithProviders = RouterModule.forChild(moduleBRoutes); 

试图导航到:本地主机:999 /功能-B成功

试图导航到:本地主机:999 /功能-B/C失败

https://plnkr.co/edit/wFlJoDXRmAlMOswmwWFk?p=preview

回答

1

工作DEMOhttps://plnkr.co/edit/xAGjXqpmkLbCokvCt9qQ?p=info

删除的

pathMatch:'full' 

不必要的使用,不要没有任何理由在任何地方使用它。

它现在按预期工作。

了解pathMatch这里https://angular.io/docs/ts/latest/guide/router.html#!#redirecthttps://angular.io/docs/ts/latest/api/router/index/Routes-type-alias.html

+0

它正在工作你的运动员,但在我的环境。我得到 错误:无法匹配任何路线:'feature-b/c' 有什么想法? – almog

+0

请仔细检查plunke。如果它在笨重的工作,它会在你的环境中工作。 – micronyks

0

我有同样的问题,在我的情况下,没有错误出现,但认为犯规的负载,我可以通过这种方式解决了这个问题:

  • 这是我懒模块
const routes: Routes = [ 
    { 
    path: '' 
    }, 
    children: [ 
     { path: '', component: AdminComponent }, 
     { path: 'users', component: AdminUsersComponent }, 
     { path: 'games', component: AdminGamesComponent }, 
     { path: '**', redirectTo: '' } 
    ] 
    } 
]; 

@NgModule({ 
    imports: [RouterModule.forChild(routes)], 
    exports: [RouterModule] 
}) 
export class AdminRoutingModule { } 
  • 这是我的根模块
const routes: Routes = [ 
    { 
    path: 'admin', loadChildren: 'app/admin/admin.module#AdminModule' 
    } 
]; 

@NgModule({ 
    imports: [RouterModule.forRoot(routes, { enableTracing: !environment.production })], 
    exports: [RouterModule] 
}) 
export class AppRoutingModule { } 

现在的路线:

  • 管理/
  • 管理/用户
  • 管理员/游戏

作品完美

相关问题