我对 Angular 项目的路由有一些问题,主要是第三级子路由。在应用程序上导航时,这些路线运行良好。( routerLink
) 和通过浏览器 URL 访问 URL 时出现问题。
我的 Angular 版本是 2.4.0
我正在使用ng serve
.
对于具有给定结构的项目,
.
├── photos
├── posts
├── users
│ ├── detail
│ │ ├── address
│ │ ├── family
│ │ ├── information
│ │ └── phones
│ ├── friends
│ └── profile
└── videos
以下是代码,
// user routes
export const userRoutes : Routes = [
{
path: 'detail',
component: DetailComponent
},
{
path: 'friends',
component: FriendsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
//app routes
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'photos',
component: PhotosComponent
},
{
path: 'posts',
component: PostsComponent
},
{
path: 'users',
component: UserListComponent
},
{
path: 'user/:username',
component: UserComponent,
children: userRoutes
},
{
path: 'videos',
component: VideosComponent
}
]
export const AppRoutes = RouterModule.forRoot(appRoutes);
export const appRoutingProviders: any[] = [];
并注册为,
@NgModule({
declarations: [
// declarations
],
imports: [
AppRoutes
],
providers: [
appRoutingProviders
],
bootstrap: [AppComponent]
})
export class AppModule { }
所以应用程序与 RouterLink 配合得很好
[routerLink]="['/user', username, 'detail']
但是当我浏览浏览器时<host>/user/myuser/detail
,它会引发异常
Uncaught (in promise): Error: Cannot find primary outlet to load 'DetailComponent'
怎么了?谢谢。
注意:我已经设置<router-outlet>
并且在 routerLink 上运行良好,当在浏览器中浏览完整 url 时出现问题。