我對Angular項目的路線有些問題,主要是第三級子路線。這些路線在應用程序導航時正在工作。 (routerLink
),並通過瀏覽器URL訪問URL時出現問題。Angular 2路線在瀏覽器瀏覽器時無法正常工作
我的角度版本是2.4.0
我在dev服務器上測試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上運行完美,並且在瀏覽器中瀏覽完整網址時出現問題。
你'中添加的'<路由器出口>路由器出口>根分量模板?如果沒有將它放入你的'AppComponent'模板中,HTML將加載 –
我在應用程序組件上有多個'router-outlet',一個在用戶組件上,另一個在詳細組件 – Marty