angular - 如何使用 :id parameter in Angular2 创建子路由

标签 angular angular2-routing

我有以下 routeConfig,此路由正常工作:

export const routeConfig: Routes = [
    {path: '', redirectTo: 'use-cases', pathMatch: 'full'},
    {path: 'use-cases', component: UseCasesComponent},
    {path: 'add', component: AddComponent},
    {path: '**', redirectTo: 'use-cases'},
];

如何为 use-cases/:id 添加路由?

我试过这个:

{path: 'use-cases', component: UseCasesComponent,
    children: [
        {
            path: '/:id',
            component: UseCaseComponent,
        }
    ]
},

但它给我错误

异常:未捕获( promise ):[object Object]

Error: Uncaught (in promise): [object Object]
    at resolvePromise (http://localhost:3000/polyfills.bundle.js:15073:32)
    at http://localhost:3000/polyfills.bundle.js:15050:14
    at ZoneDelegate.invoke (http://localhost:3000/polyfills.bundle.js:14837:27)
    at Object.onInvoke (http://localhost:3000/vendor.bundle.js:7133:42)
    at ZoneDelegate.invoke (http://localhost:3000/polyfills.bundle.js:14836:33)
    at Zone.run (http://localhost:3000/polyfills.bundle.js:14719:44)
    at http://localhost:3000/polyfills.bundle.js:15107:58
    at ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:14870:36)

最佳答案

不要在 path: '...' 中添加前导斜线

 path: '/:id'

应该是

 path: ':id'

关于angular - 如何使用 :id parameter in Angular2 创建子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288856/

相关文章:

angular - 通过 Angular 2 路由传递动态数据

html - Angular 按钮图标未显示

angular - 从我的组件单元测试中测试服务调用

Angular2 路由器 - 辅助路由

angular - 无法验证 Angular 5

angular - 带有嵌套模块的嵌套路由

angular - Angular2 RC7 和 angular-cli webpack 中的延迟加载

Angular 2 http ://localhost:3002/traceur 404 (Not Found)

@ngx-translate 和 ngx-translate-messageformat-compiler 的 Angular 复数化无法正确初始化

angular - 取消订阅 ReplaySubject 返回 asObservable();