-->

How to achieve multi tenant routing in angular?

2020-07-28 05:03发布

问题:

I am looking for implementing multi tenant routing mechanism where the URL should look like:

myapp.com/tenant-1/dashboard, 
myapp.com/tenant-2/dashboard.

回答1:

You'll have to declare your routing like this, where /:tenant_id will be dynamically generated.

{ 
    path: 'tenant/:tenant_id', 
    children: [
       { path: 'dashboard', component: DashboardComponent },
       { path: 'SomeOther', component: SomeOtherComponent }
    ]
}

You'll read the :tenant_id with the below code:

this.subscription = this.activatedRoute.queryParams.subscribe((params: Params) => {
       let tenant_id = params['tenant_id'];
       console.log(tenant_id);
});


回答2:

Then your routing declaration should look like this :

{ path: ':tenant', children: [
  { path: 'dashboard', component: DashboardComponent }
]}