angular - Angular 4默认子路由不起作用
问题描述
我有具有以下结构的 angular 4 项目:
src
|- app
| |- home
| | |- home.component.ts/html/scss
| | |- home.module.ts
| | |- home.routing.ts
| | |- products
| | | |- products.component.ts/html/scss
|- app.component.ts/html/scss
|- app-routing.module.ts
|- app.module.ts
我的app.routing.module.ts
文件包含以下内容:
const routes: Route[] = [
{
path: "home", component: HomeComponent, canActivate: [AppAuthGuard]
},
{
path: '', redirectTo: AppConstants.ROUTE_LOGIN, pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: false})],
exports: [RouterModule]
})
export class AppRoutingModule {
}
并home.routing.ts
包含以下子路由:
{
path: "home",
component: HomeComponent,
children: [
{
path: '',
pathMatch: 'full',
component: HomeStatsComponent
},
{
path: 'products',
children: [
{
path: '',
pathMatch: 'full',
component: ProductsComponent
}
{
path: 'add',
component: AddProductComponent
}
]
},
]
}
在 home.module.ts 中,子路由已使用RouterModule.forChild(HomeRoutes)
. 在 home.component.html 中有一个子路由的路由器出口。除了默认子路由 ( http://localhost:4200/home
) 之外的所有路由都成功运行(http://localhost:4200/home/products
运行正常)。http://localhost:4200/home
没有错误,它只是显示空白区域,因为路由器出口是空的。但是,如果我在其中定义主组件的默认子路由,app.module.ts
它就可以正常工作。我做错了什么让它不起作用?
解决方案
能不能直接去掉一级配置试试。由于您已经将它路由到home
,它正在路由中找到/home
,/home
因此根据配置它成为http://localhost:4200/home/home
[
{
path: '',
pathMatch: 'full',
component: HomeStatsComponent
},
{
path: 'products',
children: [
{
path: '',
pathMatch: 'full',
component: ProductsComponent
}
{
path: 'add',
component: AddProductComponent
}
]
}
]
推荐阅读
- node.js - 如何分隔 IPC 值数组
- javascript - 在进入视口时使幻灯片切换 div 内的图像淡入
- xcode - 在我的 iOS Xcode 项目中,添加到 .gitignore 的文件仍然保留并在 project.pbxproj 文件中更新
- java - jOOQ:在特定表中查找约束
- angular - 如何将 HTML 元素引用传递给 Angular 中的组件?
- amazon-web-services - domain.com:port 的 Nginx 反向代理
- regex - 至少有一个 1 和偶数个 0 的二进制字符串的正则表达式
- rest - 访问令牌的 Apex Rest 标注
- javascript - 将远程html文件加载到grapesjs中
- javascript - 获取“pdfmake”js库生成的“blob”数据并通过ajax(Laravel Controller)发送到服务器