此錯誤與 lodash 有關。我在這里添加了 _.intersection 函式,但不知道是什么導致了錯誤。任何幫助將不勝感激。
import { Directive, OnDestroy, TemplateRef, ViewContainerRef,Input } from "@angular/core";
import { Subscription } from "rxjs";
import { User } from "./model/user";
import { AuthService } from "./services/auth.service";
import * as _ from "lodash";
@Directive({
selector:'[AllowRbac]'
})
export class rbacDirective implements OnDestroy{
userInfo:User;
roles:string[];
sub:Subscription
constructor(private tempRef:TemplateRef<any>,private ViewContainer:ViewContainerRef,
private authService:AuthService){
this.sub=this.authService.user$.subscribe(res=>{
this.userInfo=res
})
}
//to fetch
@Input()set AllowRbac(roles:string[]){
this.roles=roles;
this.showAdminUI()
}
showAdminUI(){
if(!this.roles ||this.roles.length===0|| !this.userInfo){
this.ViewContainer.clear();
return
}
const roles=_.intersection(this.roles,this.userInfo.roles).length > 0
if(roles){
this.ViewContainer.createEmbeddedView(this.tempRef)
}
else{
this.ViewContainer.clear()
}
}
ngOnDestroy() {
this.sub.unsubscribe()
}
}
這是為特定用戶激活基于角色的身份驗證的指令。根據登錄的用戶,例如管理員或普通用戶隱藏 UI 材料
uj5u.com熱心網友回復:
我找到了一種解決方案,如下所示:它的作業!
"@types/lodash": "4.14.149",
感謝 ABarachugov 重播:參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532578.html
標籤:有角度的罗达什
上一篇:如何從超類方法呼叫基類方法
