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

查看:23
本文介绍了如何在 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'}
]

和一个子组件:

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

这当然失败了:group"的路由生成器未包含在传递的参数中.我不知道如何将一些默认参数传递给第一条路线.我确实通过创建另一个没有参数的路由并将其用作默认值来解决它:

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'}
]

另见

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

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