我是 Angular 2 的新手,对于我是否以正确的方式解决这个问题有点困惑。我正在编写一个 Multi-Tenancy 应用程序,我希望所有 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;
}
}
用户会看到一个租户列表并选择一个。这会在 RoutingService
上设置 ClientId
。
在每个组件中,我在构造函数中导入此 RoutingService
并且 HTML 使用它来创建路由:
[routerLink]="[routingService.getRoute('/product/list')]
有更好的方法吗?
最佳答案
我和你的情况类似,由于组件封装/隔离,目前似乎可以从服务中提取它:
Angular 2: How to pass route parameters to subroute?
https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters
我没有创建 RouteService 或 RouteParamService,而是将其烘焙到我的 AuthService 中,因为租户是身份验证的一部分。在我的案例中,除了租户之外,路由参数在孤立情况下也能正常工作。
关于angular2-routing - 如何最好地将租户包含在 Angular 2 路由中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953264/