javascript - 检查对页面的访问
问题描述
我需要创建一个方法来检查数据访问是否等于 next.data.access。我可以使用包含方法,因为它是一个数组。数组中只有一个数据访问值可以返回 true
auth.guards.ts
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return from(this.authService.me().then(data => {
let access = data['role']['unlocks'];
if('access' in next.data) {
let eachAccess = next.data.access.forEach(each => {
each.includes(access);
} );
let hasAccess = eachAccess === next.data.access;
console.log(hasAccess)
if (hasAccess) {
return next.component !== LoginComponent;
} else {
console.warn('User doesn\'t have access to this page.');
this.router.navigate(['/selection']);
return false;
}
路由模块.ts
{ path: 'admin', canActivate:[AuthGuard], component: AdminComponent, data: {access : ['ALL']}, children: [
{ path: 'manageUsers', component: ManageUserComponent}
]},
{ path: 'client', canActivate:[AuthGuard], data: {access : ['ALL','CLIENT']}, children: [
{ path: '', component: DashboardComponent },
{ path: 'dashboard', component: DashboardComponent},
{ path: 'greenhouse', component: GreenhouseComponent},
{ path: 'recipe', component: RecipeComponent},
{ path: 'greenhouse/mylamps', component: MylampsComponent},
]},
{ path: 'tools', canActivate:[AuthGuard], data: {access : ['ALL','TOOLS']}, children: [
{ path: '', component: ToolsComponent },
{ path: 'information', component: UserInformationComponent},
{ path: 'light', component: LightComponent},
{ path: 'spectrum', component: RefSpectrumComponent},
{ path: 'test', component: TestComponent},
{ path: 'progress', component: InProgressComponent}
]},
解决方案
为此,您不能使用forEach
它,因为它不会返回任何东西。
const arr = arr = [1, 2, 3];
const res = arr.forEach(v => v *2);
console.log(res); // undefined
您可以改用Array.prototype.includes():
const hasAccess = next.data.access.includes(access);
或者,如果您想搜索更复杂的内容,可以使用Array.prototype.some():
const hasAccess = next.data.access.includes(acc => acc === access);
推荐阅读
- oracle-apex - Oracle APEX表单上表,通过表单更改值后不要重置表分页
- c# - 如何实现动态加载新产品的工厂模式(开闭原则)?
- ar.js - 基于位置的 ARJS 示例不断显示警报('无法检索 GPS 位置。信号不存在。')
- python - Scrapy 响应 url 与我在启动 url 上定义的不完全一样
- python - Dask Dataframe 中的 Groupby、Unstack 和 Plot
- javascript - 无法重现错误:没有“新”就无法调用类构造函数 DateTimeFormat
- python - 模型要么未安装,要么是抽象的 - Django、Python
- android - 如何使用okhttp创建数据流并写入android中的设备
- javascript - babel-jest : this.setDynamic 不是一个函数(迁移到 Jest 25)
- javascript - javascript中奇怪的字符串值