2016-08-24 48 views
0

我目前尝试在我的应用程序中获得好的路线,但在我目前的实施中,我必须获取并检查我的路线订阅中的所有参数。有没有什么办法可以从我可以改变路线的employeesdepartments'静态'参数,而不是检查每个可能性的所有参数值?
如果我的任何路线不合逻辑或者错误,我也可以改进。Angular2 Hot从网址获取所有参数?

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 
    }, 

] 
} 
]; 

回答

3

您可以使用ActivatedRoute。这不仅是为了获得参数。例如:

ngOnInit() { 
    let path = this.route.url.value.map(val => val.path); 
    } 

将给你url的部分作为字符串数组。例如["employees","department","management"].

+0

谢谢。那正是我所寻找的。 – moessi774

1

解析您的网址是这样的:

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位于下: 根 - >孩子 - >主体 - >段

你可以阅读更多right here