【发布时间】:2020-12-10 22:30:15
【问题描述】:
我在一些路由中使用 AuthGuard:
{
path: "dashboard",
component: DashboardComponent,
pathMatch: "full",
canActivate: [RoleGuardService],
data: {
expectedRole: Role.ROLE_ADMIN,
},
},
还有我的 roleguard.service
@Injectable()
export class RoleGuardService implements CanActivate {
constructor(public auth: AuthService, public router: Router) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
const expectedRole = route.data.expectedRole;
if (!this.auth.hasRole(expectedRole)) {
this.router.navigate(["login"], {
queryParams: { returnUrl: state.url },
});
return false;
}
return true;
}
}
如果我登录应用程序,然后使用 UI 导航到仪表板,一切都会按预期工作。如果我在浏览器中输入 URL localhost:4200/dashboard 我会得到一个空白页。
我错过了什么?
编辑:错字
编辑:这是我当前的 authService:
public hasRole(r: string): boolean {
let hasRole = false;
this.getCurrentRoles().forEach((role) => {
if (role.authority === r) {
hasRole = true;
}
});
return hasRole;
}
【问题讨论】:
标签: angular auth-guard