Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 当我用防护装置阻止布线时出现角度4错误_Angular - Fatal编程技术网

Angular 当我用防护装置阻止布线时出现角度4错误

Angular 当我用防护装置阻止布线时出现角度4错误,angular,Angular,在我的angular 4项目中,我需要根据用户登录的角色来阻止一些操作。 因此,在我的routing guard中,我有一个函数,可以检索记录的用户角色并检查页面是否允许此角色,如果页面不允许,我需要路由到403页面,并返回一个observable.of(false)。 但现在在控制台中,我看到一个错误: 错误:Uncaught(在promise中):TypeError:您在预期流的位置提供了“undefined”。您可以提供一个可观察的、承诺的、数组的或可观察的。 TypeError:您在需要

在我的angular 4项目中,我需要根据用户登录的角色来阻止一些操作。 因此,在我的routing guard中,我有一个函数,可以检索记录的用户角色并检查页面是否允许此角色,如果页面不允许,我需要路由到403页面,并返回一个
observable.of(false)
。 但现在在控制台中,我看到一个错误:

错误:Uncaught(在promise中):TypeError:您在预期流的位置提供了“undefined”。您可以提供一个可观察的、承诺的、数组的或可观察的。 TypeError:您在需要流的位置提供了“undefined”。您可以提供一个可观察的、承诺的、数组的或可观察的

我需要将
Observable
返回到我的canActivateChild函数,我需要使用此版本,而不是
:boolean
版本

这是guard.ts中的功能

if (SidebarComponent.roles === undefined) {
          console.log('SidebarComponent.roles UNDEFINED')
          return this.authenticationService.getLoggedUser()
          .flatMap((res) => {
              console.log(res)
              SidebarComponent.roles = res.body.roles
              if (SidebarComponent.roles.includes(this.translateRole(role))) {
                   console.log('ALLOWED')
                   return Observable.of(true);
               } else {
                    this.routeToUnauthorized();
                    }
              })


  routeToUnauthorized() {
    this.router.navigate(['/403']);
    console.log('role NOT allowed')
    return Observable.of(false);
  }
这是loggedUser函数,用于检索有关已登录用户(角色)的信息

getLoggedUser(){
const params=new HttpParams().set('projection','withWorkShift')
返回此.endPointUrlService.checkIfMapIsReady(此.entityLink[0])
.flatMap((分辨率)=>{
返回this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.mapNames.get('usersSearchMap'))
.get(this.entityLink[0].endPoints[0]),{observe:'response',params:params})
.map((响应)=>响应);
})
}

您必须在平面图中返回一个可观察到的值

...     
} else {
     return this.routeToUnauthorized(); <- here
}
。。。
}否则{

返回这个.routeToUnauthorized();您必须在平面图中返回一个可观察到的值

...     
} else {
     return this.routeToUnauthorized(); <- here
}
。。。
}否则{

返回此.routeToUnauthorized();这里有一种使用承诺解决此问题的方法

interface IUserPacket {
    /*  define an user interface */
    body: IRoles[]
}

interface IRoles {
    /* Define your roles interface */
}


@Injectable()
export class IsAuthorized implements CanActivate {
    constructor(private authenticationService: AuthenticationService, private router: Router) { }

    public canActivate(route: ActivatedRouteSnapshot): boolean | Observable<boolean> | Promise<boolean> {
        const promise = new Promise<boolean>((resolve, reject) => {

            if (SidebarComponent.roles === undefined) {

                this.authenticationService.getLoggedUser().then((userPacket: IUserPacket) => {
                    userPacket.body.roles

                });

                SidebarComponent.roles = res.body.roles
                if (SidebarComponent.roles.includes(this.translateRole(role))) {
                    console.log('ALLOWED')
                    resolve(true);
                } else {
                    this.router.navigate(['/403']);
                    console.log('role NOT allowed')
                    resolve(false);
                    // reject();
                }
            }
        });

        return promise;
    }

    private getLoggedUser(): Promise<IUserPacket> {
        return new Promise<IUser>((resolve, reject) => {
            const params = new HttpParams().set('projection', 'withWorkShift')

            const url = this.endPointUrlService.cutLinks(this.endPointUrlService.mapNames.get('usersSearchMap'))

            return this.http.get(url).map(resolve);
        })
    }
}
接口数据包{
/*定义用户界面*/
正文:IRoles[]
}
界面气孔{
/*定义您的角色界面*/
}
@可注射()
导出类已授权实现可激活{
构造函数(私有authenticationService:authenticationService,私有路由器:路由器){}
公共canActivate(路由:ActivatedRouteSnapshot):布尔值|可观察|承诺{
持续承诺=新承诺((解决、拒绝)=>{
if(SidebarComponent.roles==未定义){
this.authenticationService.getLoggedUser()。然后((userPacket:IUserPacket)=>{
userPacket.body.roles
});
SidebarComponent.roles=res.body.roles
if(SidebarComponent.roles.includes(this.translateRole(role))){
console.log('ALLOWED')
决心(正确);
}否则{
this.router.navigate(['/403']);
console.log('不允许角色')
决议(假);
//拒绝();
}
}
});
回报承诺;
}
私有getLoggedUser():Promise{
返回新承诺((解决、拒绝)=>{
const params=new HttpParams().set('projection','withWorkShift')
const url=this.endPointUrlService.cutLinks(this.endPointUrlService.mapNames.get('usersSearchMap'))
返回此.http.get(url).map(解析);
})
}
}

这里有一种使用承诺解决问题的方法

interface IUserPacket {
    /*  define an user interface */
    body: IRoles[]
}

interface IRoles {
    /* Define your roles interface */
}


@Injectable()
export class IsAuthorized implements CanActivate {
    constructor(private authenticationService: AuthenticationService, private router: Router) { }

    public canActivate(route: ActivatedRouteSnapshot): boolean | Observable<boolean> | Promise<boolean> {
        const promise = new Promise<boolean>((resolve, reject) => {

            if (SidebarComponent.roles === undefined) {

                this.authenticationService.getLoggedUser().then((userPacket: IUserPacket) => {
                    userPacket.body.roles

                });

                SidebarComponent.roles = res.body.roles
                if (SidebarComponent.roles.includes(this.translateRole(role))) {
                    console.log('ALLOWED')
                    resolve(true);
                } else {
                    this.router.navigate(['/403']);
                    console.log('role NOT allowed')
                    resolve(false);
                    // reject();
                }
            }
        });

        return promise;
    }

    private getLoggedUser(): Promise<IUserPacket> {
        return new Promise<IUser>((resolve, reject) => {
            const params = new HttpParams().set('projection', 'withWorkShift')

            const url = this.endPointUrlService.cutLinks(this.endPointUrlService.mapNames.get('usersSearchMap'))

            return this.http.get(url).map(resolve);
        })
    }
}
接口数据包{
/*定义用户界面*/
正文:IRoles[]
}
界面气孔{
/*定义您的角色界面*/
}
@可注射()
导出类已授权实现可激活{
构造函数(私有authenticationService:authenticationService,私有路由器:路由器){}
公共canActivate(路由:ActivatedRouteSnapshot):布尔值|可观察|承诺{
持续承诺=新承诺((解决、拒绝)=>{
if(SidebarComponent.roles==未定义){
this.authenticationService.getLoggedUser()。然后((userPacket:IUserPacket)=>{
userPacket.body.roles
});
SidebarComponent.roles=res.body.roles
if(SidebarComponent.roles.includes(this.translateRole(role))){
console.log('ALLOWED')
决心(正确);
}否则{
this.router.navigate(['/403']);
console.log('不允许角色')
决议(假);
//拒绝();
}
}
});
回报承诺;
}
私有getLoggedUser():Promise{
返回新承诺((解决、拒绝)=>{
const params=new HttpParams().set('projection','withWorkShift')
const url=this.endPointUrlService.cutLinks(this.endPointUrlService.mapNames.get('usersSearchMap'))
返回此.http.get(url).map(解析);
})
}
}