角2路由,增加孩子的路线导致路由无法工作
问题描述:
田地使用最新版本的角2路玩,我注意到,在下方加入不是一个孩子的路线原因/管理员上班路线角2路由,增加孩子的路线导致路由无法工作
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AdminComponent } from './admin.component';
import {AdminChildComponent} from './admin-child.component';
import {productRoutes} from "../product/product.routes";
export const adminRoutes : Routes =[
{
path: 'admin', component: AdminComponent,
children:[
{path:"/child", component:AdminChildComponent}
]
}
];
export const adminRouting: ModuleWithProviders = RouterModule.forChild(adminRoutes);
一旦我下面去掉,应用wors就好了,只要我添加它,我得到的控制台错误
EXCEPTION: Uncaught (in promise): Error: Cannot match any routes: 'admin'
children:[
{path:"/child", component:AdminChildComponent}
]
需要帮助的可能理解为什么我得到这个错误。完全混淆。
答
这是因为:
由于这清楚地看到,AdminComponent有孩子。这意味着AdminComponent将有路由器插座模板中的某处。
现在的问题是,每当路由器出口使用,它需要一个视图。在你的路由情况下,它没有任何适当/主要的子路由。因此,你需要将其设置如下图所示,
export const adminRoutes : Routes =[
{
path: 'admin', component: AdminComponent,
children:[
{ path: '', redirectTo: 'child', pathMatch: 'full'}, //<----- here
{path:"child", component:AdminChildComponent}
]
}
];
注意:改变/child
到child
。为什么?因为angular2路由器本身在父路由和子路由之间添加/
。所以在这里你不需要/
。
我不明白为什么需要这样的配置。尽管如此,添加你建议的{path:'',...}会产生一个异常EXCEPTION:未捕获(承诺):错误:无法匹配任何路由:'child' – iswak
我在回答中给出了解释为什么这样的路由是必需的。 – micronyks
我理解你的解释,并将你的答案考虑在内,并与其他具有儿童路线的路线进行比较。是的,看起来你总是需要一条空的路线,或者重定向或者路由到一个组件。我的问题是“/孩子”而不是“孩子” – iswak