我目前尝试在我的应用程序中获得不错的路由,但在我当前的实现中,我必须获取并检查路由订阅中的所有参数。有没有什么方法可以从路线中获得employees
或departments
的“静态”参数,我可以提前做出改变,而不是检查所有参数值的每种可能性?
如果我的任何路线都不合逻辑或只是错误的话,我也愿意接受改进。
export const EmployeeManagementRoutes: RouterConfig = [
{
path: 'employee-management',
component: EmployeeManagementComponent,
children: [
//display all employees
{
path: '',
//is there any way to make this redirict relative to the component the router is for?
redirectTo: '/employee-management/employees/department/all',
pathMatch: 'full'
},
//display all employees from a specific department
{
path: 'employees//department/:department',
component: EmployeeManagementTableComponent
},
//open dialog for deleting or creating a new employee or
if option is 'show' and id is not undefined show a specific employee
{
path: 'employees/action/:option/:id',
component: EmployeeManagementTableComponent
},
//display all departments
{
path: 'departments',
component: EmployeeManagementTableComponent
},
//open dialog for deleting or creating a new department
{
path: 'departments/action/:option',
component: EmployeeManagementTableComponent
},
]
}
];
答案 0 :(得分:3)
您可以使用ActivatedRoute
。这不仅仅是为了获得参数。类似的东西:
ngOnInit() {
let path = this.route.url.value.map(val => val.path);
}
会将url的部分作为字符串数组提供给你。例如["employees","department","management"].
答案 1 :(得分:2)
解析你的网址:
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
export class TopNavigationComponent implements OnInit {
constructor(
private router: Router
) {}
ngOnInit() {
let parsedUrl = this.router.parseUrl(this.router.url);
console.log(parsedUrl);
}
}
在收到的对象中,解析的URL位于: 根 - > CHILDREN->伯 - >段
您可以阅读更多here