I have an Angular application with the structure as on the image:
In want to conditionally select one of the themes based on the data retrieved from the server.
const routes: Routes = [
{
path: '',
loadChildren: 'app/presentation/theme1/theme1.module#Theme1Module',
canActivate: [Theme1Guard],
},
{
path: '',
loadChildren: 'app/presentation/theme2/theme2.module#Theme2Module',
canActivate: [Theme2Guard],
}
];
Both theme-1
and theme-2
modules have the same routes to similar component with different layout and styles.
UPDATE 1
I tried CanActivate
guards one for theme-1
and the second for theme-2
. Each guard retrieves current theme name from the themeStore
and compares it to the current route:
canActivate() {
let currentTheme: string = '';
this.themeStore.currentTheme.subscribe((themeName) => {
currentTheme = themeName;
});
if (currentTheme == 'theme1') {
return true;
}
else {
return false;
}
}
However, this won't work because Angular router does not look for the same path after the first one was rejected by CanActivate
guard.
UPDATE 2
There's an open issue in Angular repository - Load a component in a route depending on an asynchronous condition. It seems to be added to backlog a few months ago.