0

Thats my html file:

  <a [routerLink]="['tests/...']">Tests</a>

This terminal route is already defined the parent component Routes configuration.

Due to maintaining my software I would like to declare kind of a constant string and re-use it from my Routes and routerLink.

How can I do that ?

4

1 回答 1

0

您不能直接从组件的模板访问常量。

您可以注入一个服务来访问您的“全局”值,例如

@Injectable() 
export class RoutePaths {
  test:string = 'tests/...';
  other:string = 'other';
}

@Component({
  selector: 'some-comp',
  template: `
<a [routerLink]="[routePaths.test]">Tests</a>
<a [routerLink]="[routePaths.other]">Tests</a>
`})
export class SomeComponent {
  constructor(private routePaths:RoutePaths) {}
}

bootstrap(AppComponent, [RoutePaths]);
于 2016-06-01T05:45:53.447 回答