防止在 routerLink 中重复使用 path/route

Prevent the duplciate path/route usage in the routerLink

那是我的 html 文件:

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

此终端路由已定义父组件路由配置。

由于维护我的软件,我想声明一种常量字符串并从我的路由和路由器链接中重新使用它。

我该怎么做?

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

您可以注入一个服务来访问您的 "global" 值,例如

@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]);