2016-08-24 4 views
0

私は現在、自分のアプリケーションで素敵なルートを取得しようとしていますが、私の現在の実装では、私のルートサブスクリプションのすべてのパラメータを取得してチェックする必要があります。 employeesまたはdepartmentsというような、すべての可能性についてすべてのパラメータ値をチェックするのではなく、前もって差をつけることができる「静的」パラメータを得る方法はありますか?
私のルートのいずれかが非論理的であるか間違っている場合、改善のために開かれています。Angular2 URLからすべてのパラメータを取得するにはホットですか?

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を使用できます。 paramsを取得するだけではありません。何かのように:

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

あなたの文字列配列としてURLの部分を与えるでしょう。解析されたURLが下に配置されて受信したオブジェクトで

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); 
    } 
} 

:インスタンス["employees","department","management"].

+0

ありがとうございます。それがまさに私が探し求めたものでした。 – moessi774

1

のために、このようなあなたのURLを解析 root-> children-> 1次側>セグメント

あなたはより多くを読むことができます右here

関連する問題