I need to guard the routerLinks based on some boolean configurations coming from a server. Currently i have written canActivate
route guard. ActivateRoutes
file
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {
const visibility = route.data.name;
return await this.service.activateRouterLinks(visibility);
}
This my app.module.ts file
{
path: 'home',
loadChildren: 'src/app/home/home.module#HomeModule',
data: {
title: 'Home', name: Names.HOME
},
canActivate: [ActivateRoutes]
},
But actually what i want is i want to pass a boolean value to data like this instead of name.
data: {
title: 'Home', state: homePageActive
},
that coming from my service class (the route guard). Currently I am using a map and every time page loads it will search all the map to find the name. Therefore I want to set for a boolean value at once.
This is my service class code.
linkNames = new Map<string, []>();
homePageActive = false;
getConfigurations() {
this.getUIConfigurations().subscribe(
configs => {
for (const value of configs.names) {
this.linkNames.set(value.name, value.secondary_tabs);
}
mapNames();
});
}
mapNames() {
if (this.linkNames.has(Names.HOME)) {
homePageActive = true;
}
}
asyn cactivateRouterLinks(visibility) {
await this.waitSecond();
if (this.linkNames.has(visibility)) {
return true;
} else {
this.router.navigateByUrl('/page_not_found');
return false;
}
}
waitSecond() {
return new Promise(resolve => {
setTimeout(() => {
resolve('return aftersecond!');
}, 160);
});
}