Angular 2路由 - 无法匹配URL段中的任何路由
问题描述:
我已经失去了7天以上,但无法使其工作。当我将路径放入children
中的模板时,出现错误,应用程序崩溃。Angular 2路由 - 无法匹配URL段中的任何路由
这里是我的代码:
export const routes: Routes = [
{path: 'store', component: StoreComponent,
children: [
{path: 'apple', component: AppleComponent}
]
}
AppleComponent.ts
import {Component, OnInit, Type} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
@Component ({
selector: 'apple',
templareUrl: '../apple.html
})
export class AppleComponent implements OnInit {
constructor(){}
nGOnInit():void{}
}
我已经把<base href="/">
index.html文件中,但没有帮助。
我刚开始学习Angular2,所以请原谅我,如果我问一个愚蠢的问题。
编辑:这将需要去children
,因为router-outlet
。
谢谢你们。
答
您可以简单地使用这个
{ path: 'store/apple', component: AppleComponent, pathMatch: 'full' }
另外,您可以用这种方式,
export const storeChildrenRoutes: Routes = [
{
path: 'store',
component: StoreComponent,
children: [
{
path: 'apple',
component: AppleComponent,
}
]
}
];
@NgModule({
imports: [
RouterModule.forChild(storeChildrenRoutes)
],
exports: [
RouterModule
]
})
export class StoreChildRoutingModule {}
有此作为一个独立的模块,并导入此到AppRouting模块。 你AppRouting模块将包含你的主要路线有
RouterModule.forRoot(appRootRoutes)
是的我可以,但我需要它,因为路由器插座的孩子。 –
你在'StoreComponent'里面使用 –
Aravind
不,你会怎么做到这一点? –