Angular 5个子路由在路由中添加括号
Angular 5 child routes adding parentheses to the route
我有一个包含一系列组件的 Angular 5 应用程序。有些组件是其他组件的子组件,有些则不是。
我希望应用程序具有如下结构:
*/my-account/overview // homepage
*/my-account/my-stuff
*/profile
*/non-default
我有一个 DefaultComponent,它包含一些通用元素,例如我希望出现在大多数页面上的站点导航(除了 */non-default route/component 之外的所有元素)。
我的路由模块定义了以下路由:
export const routes: Routes = [
{ path: '', redirectTo: 'my-account', pathMatch: 'full' }, // should redirect to localhost:4200/my-account
{ path: '', component: DefaultComponent, children: [
{ path: 'my-account', children: [
{ path: '', redirectTo: 'overview', pathMatch: 'full' }, // should redirect to localhost:4200/my-account/overview
{ path: 'overview', component: OverviewComponent }, // should resolve: localhost:4200/my-account/overview
{ path: 'my-stuff', component: MyStuffComponent } // should resolve: localhost:4200/my-account/my-stuff
]},
{ path: 'profile', component: ProfileComponent } // should resolve: localhost:4200/profile
]},
{ path: 'non-default', component: NonDefaultComponent } // should resolve: localhost:4200/non-default
];
如果我直接在浏览器中点击 URL,这些路由将完美解析。我遇到的问题是,当我尝试使用 [routerLink] 指令渲染锚点时,实际应用于 href 的 URL 是
href="/my-account/overview/(profile)"
而不是
href="/profile"
对于 ProfileComponent。因此,如果我使用 [routerLink] 呈现导航项目列表,每个项目都有一个 href,但实际上并没有解析到该组件。
如果有帮助,我有一个Github repository demonstrating the issue here。
我是否错误配置了路由?
正如 Eliseo 正确指出的那样。我在 RouterLink 本身的路由开头缺少斜杠。
[routerLink]=['my-account/overview']
应该是
[routerLink]=['/my-account/overview']
值得注意的是,在我引入子路由之前它一直运行良好。
我有一个包含一系列组件的 Angular 5 应用程序。有些组件是其他组件的子组件,有些则不是。
我希望应用程序具有如下结构:
*/my-account/overview // homepage
*/my-account/my-stuff
*/profile
*/non-default
我有一个 DefaultComponent,它包含一些通用元素,例如我希望出现在大多数页面上的站点导航(除了 */non-default route/component 之外的所有元素)。
我的路由模块定义了以下路由:
export const routes: Routes = [
{ path: '', redirectTo: 'my-account', pathMatch: 'full' }, // should redirect to localhost:4200/my-account
{ path: '', component: DefaultComponent, children: [
{ path: 'my-account', children: [
{ path: '', redirectTo: 'overview', pathMatch: 'full' }, // should redirect to localhost:4200/my-account/overview
{ path: 'overview', component: OverviewComponent }, // should resolve: localhost:4200/my-account/overview
{ path: 'my-stuff', component: MyStuffComponent } // should resolve: localhost:4200/my-account/my-stuff
]},
{ path: 'profile', component: ProfileComponent } // should resolve: localhost:4200/profile
]},
{ path: 'non-default', component: NonDefaultComponent } // should resolve: localhost:4200/non-default
];
如果我直接在浏览器中点击 URL,这些路由将完美解析。我遇到的问题是,当我尝试使用 [routerLink] 指令渲染锚点时,实际应用于 href 的 URL 是
href="/my-account/overview/(profile)"
而不是
href="/profile"
对于 ProfileComponent。因此,如果我使用 [routerLink] 呈现导航项目列表,每个项目都有一个 href,但实际上并没有解析到该组件。
如果有帮助,我有一个Github repository demonstrating the issue here。
我是否错误配置了路由?
正如 Eliseo 正确指出的那样。我在 RouterLink 本身的路由开头缺少斜杠。
[routerLink]=['my-account/overview']
应该是
[routerLink]=['/my-account/overview']
值得注意的是,在我引入子路由之前它一直运行良好。