このようなことが必要な場合は、自分で簡単に作成できます。エクスポートしてルートを定数として作成し、使用したい場所にインポートすることができます。
ステップ1
あなたのルートをエクスポートする場所routes.ts
と呼ばれるファイルを作成します。
export const HOME = 'home';
export const ABOUT = 'about';
export const CONTACT = 'contact';
ステップ2
あなたがルートを使いたいところはどこでもあなたのroutes.ts
ファイルをインポートし、あなたのルートを参照してください:
import * as routes from './routes';
例1 app.routing.ts:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import * as routes from './routes';
const routes: Routes = [
{
path: routes.HOME,
component: HomeComponent
},
{
path: routes.ABOUT,
component: AboutComponent
},
{
path: routes.CONTACT,
component: ContactComponent
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
例2 home.component.tsとhome.component.html:
はあなたhome/about/contact.component.ts
の内側にあなたのroutes.ts
ファイルをインポートします。
<a [routerLink]="routes.HOME">Home</a>
<a [routerLink]="routes.ABOUT">About</a>
<a [routerLink]="routes.CONTACT">Contact</a>
あなたの目標
あなたの目標のあなたの両方を達成するため、この方法は:
その後、あなたの変数の方にごhome/about/contact.component.html
内参照してください
- あなたは変数の値を変更することができます(
routes.ts
ファイル)。
- routes.ts ファイル内で定義したルートのみを使用します。そうすれば、ルートは常に機能します。
こんにちは私はあなたのための実装を追加しました、それは助けて欲しいです! – Brampage
@Brampageありがとう! – Vladyabra