Load different set of routes if user is on domain and different if not on domain

102 views
Skip to first unread message

methuselah90

unread,
Mar 27, 2020, 9:02:46 PM3/27/20
to Angular and AngularJS discussion

I want to load a different set of domains at launch if a user is on a subdomain (test.example.com), versus being on a domain (example.com). However, my code keeps defaulting to the domainRoutes without the facility to allow the user to browse (as all the routes don't exist). How can I fix this?


const routes: Routes = [
{
matcher: (url) => {
if (isOnDomain()) {
return { consumed: url };
}
return null;
},
loadChildren: './modules/website/website.module#WebsiteModule'
},
{
matcher: (url) => {
if (!isOnDomain()) {
return { consumed: url };
}
return null;
},
loadChildren: './modules/main/main.module#MainModule'
}
];

const isOnDomain = () => {
const full = window.location.host;
const parts = full.split('.');
let result = true;
if (parts[0] && parts[1] && parts[2]) {
result = false;
}
console.log(result);
return result;
};

@NgModule({
declarations: [
AppComponent
],
imports: [
RouterModule.forRoot(routes)
],
providers: [
],
bootstrap: [AppComponent]
})

export class AppModule {
}

Sander Elias

unread,
Apr 1, 2020, 1:03:48 AM4/1/20
to Angular and AngularJS discussion
Hi,

Interesting issue. Assuming the log of the result gives the expected result. Have you tried it with loading a couple of different components to see if that works? Also, I would add a `console.log` to the loaded modules constructors, so you can see if they are loaded, to begin with.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages