2016-08-24 47 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