2017-11-22 8 views
2

私はAngularアプリケーション全体でルート文字列をハードコードしていることに少し気を付けています。それはちょっと間違っているようです!例えば角度経路 - ハードコードされた文字列を避ける

this._router.navigate(['dashboard/customer', customer.CustomerId]); 
path: 'customer', 
component: CustomerComponent, 

これを回避する方法はありますか?その後、アプリからこれを通じ使用

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 
// import of customer component 

const routes: Routes = [ 
    { 
    path: 'customer', pathMatch: 'full', component: CustomerComponent 
    } 
    // More routes ... 
    { 
    path: '**', 
    redirectTo: '/not-found' 
    } 
]; 

@NgModule({ 
    imports: [RouterModule.forRoot(routes)], 
    exports: [RouterModule] 
}) 
export class RoutingModule { } 

答えて

1

は、ルートモジュールでルータのパスの静的変数を定義します。

0

は、私はあなたがこのテンプレートを使用することをお勧めします。たとえば:

は、ルート・パスを定義します。

export class AppRoutes { 
     public static CUSTOMER: string = "customer"; 
     public static ERROR: string = "error"; 
    } 

ルート設定:

const routes: Routes = [ 
    { 
    path: AppRoutes.CUSTOMER, component: CustomerComponent 
    } 
]; 

ナビゲーション:

this._router.navigate(["/" + AppRoutes.CUSTOMER, customer.CustomerId]); 
0

我々はルートを命名していたが、角度がベータ版にあったときにその概念が死亡しました(またはそれはRCでした)。

グローバルオブジェクトを使用して、そのプロパティとしてルートを使用することができます。または、関数で実行することもできます。

import { routes } from '../global-settings'; 

// with strings 
this._router.navigate([routes.dashboard.customer, customer.CustomerId]); 

// with functions 
this._router.navigate(routes.dashboard.customer(customer.CustomerId)); 
関連する問題