【发布时间】:2016-12-21 13:14:14
【问题描述】:
我对 Angular 2 还很陌生,对于我是否以正确的方式处理这个问题有点困惑。我正在编写一个多租户应用程序,我希望所有 url 都以租户 ID 开头。例如:https://example.org/tenant-1/product/list 或 https://example.org/tenant-2/product/list。这似乎是一个非常简单且解决得很好的问题,但我很难找到实现此目标的推荐方法。
到目前为止,我已经创建了一个路由服务:
@Injectable()
export class RoutingService {
public ClientId: string = null;
constructor() {}
getRoute(path: string): string {
if (!this.ClientId) {
// For deep-linking
this.ClientId = window.location.pathname.split('/')[1];
}
return '/' + this.ClientId + path;
}
}
向用户显示租户列表并选择一个。这会将ClientId 设置在RoutingService 上。
在每个组件中,我在构造函数中导入这个RoutingService,HTML 使用它来创建路由:
[routerLink]="[routingService.getRoute('/product/list')]
有更好的方法吗?
【问题讨论】:
-
您能找到解决方案吗?我有一个类似的用例。
-
我发现这个答案很有帮助。 stackoverflow.com/questions/48438786/…
标签: angular2-routing