angular2-routing - 如何最好地将租户包含在 Angular 2 路由中

我是 Angular 2 的新手,对于我是否以正确的方式解决这个问题有点困惑。我正在编写一个 Multi-Tenancy 应用程序,我希望所有 url 都以租户 ID 开头。例如:https://example.org/tenant-1/product/listhttps://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 中,因为租户是身份验证的一部分。在我的案例中,除了租户之外,路由参数在孤立情况下也能正常工作。

https://stackoverflow.com/questions/38953264/

相关文章:

java - 未找到包含 : org. eclipse.update.feature、org.ecl

python - 重构模块名称后unpickle实例

java - org.springframework.security.authentication

android - 如何在 Retrofit2 converter-simplexml 中调试 "j

javascript - d3 读取输入值

javascript - 让 Gojs FlowCart 在 AngularJS 上工作

qt - 如何获取系统的默认浏览器?

java - artemis rest 集成 - 无法使用队列中的消息

linux - 有没有办法从 git lfs 指针下载实际文件?

php - 正则表达式 yii2 电话号码验证器