如何在Angular Component Router中使用参数定义默认路由? [英] How to define default route with parameters in Angular Component Router?

查看:122
本文介绍了如何在Angular Component Router中使用参数定义默认路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的子组件中使用默认路由(使用useAsDefault: true设置),并希望将参数自动传递给它.我在文档中找不到任何方法. 我有一个具有以下路由的父组件:

I want to have a default route in my sub-component (set with useAsDefault: true) and to have parameters automatically passed to it. I cannot find anywhere in documentation how to do it. I have a parent component with a following route:

$routeConfig = [
    { path: '/employees/...', component: 'employeesComponent', name: 'Employees'}
]

和带有以下内容的子组件:

and a child component with:

$routeConfig = [
    { path: '/:group/:filter', component: 'employeeListComponent', name: 'Group', useAsDefault: true}
    { path: '/details/:employeeId/...', component: 'profileComponent', name: 'EmployeeProfile'}
]

这当然失败了: Route generator for 'group' was not included in parameters passed. 我不知道如何将一些默认参数传递给第一条路线.我确实通过创建另一条没有参数的路由并将其用作默认路由来绕过它:

This of course fails with: Route generator for 'group' was not included in parameters passed. I couldn't figure out how to pass the some default parameters to the first route. I did work arount it by creating yet another route without params, and using that as a default:

{ path: '/all', component: 'employeeListComponent', name: 'All', useAsDefault: true}

但是那当然不是理想的.到目前为止,我没有比使用没有参数的路由更好的了,然后立即使用一些默认参数进行重定向.有没有更好的方法?

but that of course is not ideal. So far, I've not come up with anything better that using route without params, and then immediately redirecting with some default parameters. Isn't there a better way?

推荐答案

据我所知,这不受支持.您可以使用带有和不带有参数的路由,然后在GroupDefault组件中导航至Group

As far as I know this is not supported. You can have a route with and without the parameter and then in the GroupDefault component just navigate to Group

$routeConfig = [
    { path: '/group', component: 'employeeListComponentDefault', name: 'GroupDefault', useAsDefault: true}

    { path: '/:group/:filter', component: 'employeeListComponent', name: 'Group'},
    { path: '/details/:employeeId/...', component: 'profileComponent', name: 'EmployeeProfile'}
]

另请参见

  • Optional Parameters While Routing angular2
  • Angular 2 optional route parameter

这篇关于如何在Angular Component Router中使用参数定义默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆