Route Guard:如何根据HttpPost设置的参数设置true或false? [英] Route Guard: How can I set true or false based on params set from HttpPost?

查看:148
本文介绍了Route Guard:如何根据HttpPost设置的参数设置true或false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

My code:

@Injectable()

export class UserRouteAccessService implements CanActivate {

    authorized = [
        'AGREEMENTS_VIEW',
        'PROSPECTS_VIEW',
        'AGREEMENTS_INSERT_UPDATE',
        'PRODUCTS_INSERT_UPDATE',
        'PROSPECTS_INSERT_UPDATE',
        'DOCUMENTS_VIEW',
        'DOCUMENTS_INSERT_UPDATE',
    ];

    constructor(private router: Router, private securityService: CralSecurityService) {
    }



    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        //boolean var


        this.securityService.securityActions().subscribe(
            data => {
                //control if auth. is correct

                //if yes set true
                //if not set false
            },
            error => {
                //return an error
            }
        )
        //return boolean
    }
}

评论是id想要做的,但是我真的不知道该写些什么,因为这是我第一次使用angular 4.

The comments are what id like to do but i cant really know what should i write since this is my first time working with angular 4.

基本上,如果要发送来自授权的某些参数,我想将其设置为True,如果服务中没有任何参数,则将其设置为false.

Basically I want to set True if some of the params from authorized are sent and false if there are no params in the service.

我希望这很清楚,如果您需要更多信息,请告诉我:)

I hope this is clear, if you need more infos tell me :)

服务:

securityActions(): Observable<any> {
    return this.http.post<Observable<any>>(
        `${this.ENDPOINT}/security-actions`,
        null,
    );
}

推荐答案

您想返回一个布尔值,但是该布尔值只能异步获取.因此,您不能返回布尔值.幸运的是,如您在方法的返回类型中所见,您可以返回Observable<boolean>.一个Observable(或一个Promise)正是用来做到这一点的:返回一个异步结果.

You want to return a boolean, but the boolean can only be obtained asynchronously. So you can't return a boolean. Fortunately, as you can see in the return type of the method, you can return an Observable<boolean>. An Observable (or a promise) is precisely used to do that: return an asynchronous result.

因此,请勿订阅securityActions()返回的可观察对象.而是使用map()catchError()运算符将可观察对象转换为可观察对象:

So, don't subscribe to the observable returned by securityActions(). Instead, transform the observable into an Observable, using the map() and the catchError() operators:

return this.securityService.securityActions().pipe(
    map(data => {
            //control if auth. is correct

            //if yes set true
            //if not set false
    },
    catchError(() => of(false))
);

注意:如果您仍在使用Angular和RxJS的旧版本,则应进行升级.如果确实还不能,则需要将上面的代码调整为所使用的RxJS的旧版本:

Note: if you're still using an old version of Angular and RxJS, then you should upgrade. If you really can't yet, you'll need to adapt the above code to the old version of RxJS you're using:

// TODO import the required operators, as documented in your old version of RxJS

return this.securityService.securityActions().map(
    data => {
            //control if auth. is correct

            //if yes set true
            //if not set false
    }
).catch(() => of(false));

这篇关于Route Guard:如何根据HttpPost设置的参数设置true或false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆