我是Angular 2的新手,对于我是否以正确的方式处理这个问题有点困惑。我正在编写一个多租户应用程序,我希望所有的网址都以租户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,并使用它来创建路由:
[routerLink]="[routingService.getRoute('/product/list')]有没有更好的方法来做这件事?
发布于 2016-08-25 04:32:03
我的情况与您类似,由于组件封装/隔离,目前似乎从服务中提取它是一种方法:
Angular 2: How to pass route parameters to subroute?
https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters
我没有创建RouteService或RouteParamService,而是将其放入我的AuthService中,因为租户是身份验证的一部分。在我的例子中,除了租户之外,路由参数在隔离状态下工作得很好。
https://stackoverflow.com/questions/38953264
复制相似问题