将 angular 路由路径提取到单独的文件中
Extract angular routing's paths into separate file
Angular 路由通常这样定义和使用:
const routes: Routes = [
{ path: "register", component: RegisterComponent },
{ path: "login", component: LoginComponent },
// more...
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export default class AppRoutingModule { }
我不想将路由移动到单独的文件 - 我只想移动路径。
所以我想提取 "register"
和 "login"
魔法字符串并将它们放在某个地方,然后执行此操作:
const routes: Routes = [
{ path: config.routes.register, component: RegisterComponent },
{ path: config.routes.login, component: LoginComponent },
// more...
];
我有一个Configuration
class我添加到DI,我把它注入到我需要的地方。如果可能的话,我想在那里添加路径。
部分选项:
- 我可以在该配置上使用静态属性 class,但那是一种 hack,更难测试。
- 我可以
const config = new Configuration();
并像上面那样在 routes
中使用它 - 但是如果它也需要成为 IOC 容器的一部分,因为它有自己的依赖项怎么办?
- 来自@DenisPupyrev 的回答:使用枚举。但是和选项 2 一样,这意味着字符串必须在一个地方编码而不需要依赖项(即没有 DI)。
这些都是不错的选择。但是提取魔法字符串最干净的方法是什么,并且还使用 DI?
在 TypeScript 中,您有很好的机会使用 "Enums"。
Routes.ts
export enum Routes {
LOGIN = 'login',
REGISTER = 'register',
...
}
app-routing.module.ts
import { Routes } from './path/to/Routes';
...
{ path: Routes.LOGIN, component: LoginComponent }
UPD:
如需DI,可使用特殊服务:
route.service.ts
@Injectable()
export class RouteService {
routes = {};
constructor() {
this.addRoute('login');
}
addRoute(route: string) {
this.routes[route] = route;
}
get ROUTES() {
return this.routes;
}
}
any.component.ts
import { RouteService } from './path/to/route.service';
...
constructor(private routeService: RouteService) {}
...
anyMethod() {
this.routeService.ROUTES.login;
}
Angular 路由通常这样定义和使用:
const routes: Routes = [
{ path: "register", component: RegisterComponent },
{ path: "login", component: LoginComponent },
// more...
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export default class AppRoutingModule { }
我不想将路由移动到单独的文件 - 我只想移动路径。
所以我想提取 "register"
和 "login"
魔法字符串并将它们放在某个地方,然后执行此操作:
const routes: Routes = [
{ path: config.routes.register, component: RegisterComponent },
{ path: config.routes.login, component: LoginComponent },
// more...
];
我有一个Configuration
class我添加到DI,我把它注入到我需要的地方。如果可能的话,我想在那里添加路径。
部分选项:
- 我可以在该配置上使用静态属性 class,但那是一种 hack,更难测试。
- 我可以
const config = new Configuration();
并像上面那样在routes
中使用它 - 但是如果它也需要成为 IOC 容器的一部分,因为它有自己的依赖项怎么办? - 来自@DenisPupyrev 的回答:使用枚举。但是和选项 2 一样,这意味着字符串必须在一个地方编码而不需要依赖项(即没有 DI)。
这些都是不错的选择。但是提取魔法字符串最干净的方法是什么,并且还使用 DI?
在 TypeScript 中,您有很好的机会使用 "Enums"。
Routes.ts
export enum Routes {
LOGIN = 'login',
REGISTER = 'register',
...
}
app-routing.module.ts
import { Routes } from './path/to/Routes';
...
{ path: Routes.LOGIN, component: LoginComponent }
UPD:
如需DI,可使用特殊服务:
route.service.ts
@Injectable()
export class RouteService {
routes = {};
constructor() {
this.addRoute('login');
}
addRoute(route: string) {
this.routes[route] = route;
}
get ROUTES() {
return this.routes;
}
}
any.component.ts
import { RouteService } from './path/to/route.service';
...
constructor(private routeService: RouteService) {}
...
anyMethod() {
this.routeService.ROUTES.login;
}