在TypeScript中,你有很好的机会使用“枚举”。
路线。ts
export enum Routes {
LOGIN = 'login',
REGISTER = 'register',
...
}
应用程序路由。单元ts
import { Routes } from './path/to/Routes';
...
{ path: Routes.LOGIN, component: LoginComponent }
UPD:
如果您需要DI,您可以使用特殊服务:
路线服务ts
@Injectable()
export class RouteService {
routes = {};
constructor() {
this.addRoute('login');
}
addRoute(route: string) {
this.routes[route] = route;
}
get ROUTES() {
return this.routes;
}
}
任何组成部分ts
import { RouteService } from './path/to/route.service';
...
constructor(private routeService: RouteService) {}
...
anyMethod() {
this.routeService.ROUTES.login;
}