Angular Route Guard:如何根据从HttpPost设置的参数设置true或false?

Angular Route Guard:如何根据从HttpPost设置的参数设置true或false?,angular,typescript,canactivate,angular-route-guards,Angular,Typescript,Canactivate,Angular Route Guards,我的代码: @Injectable() 导出类UserRouteAccessService实现CanActivate{ 授权=[ "意见",, "展望",, “协议插入更新”, “产品插入更新”, “潜在客户插入更新”, “文档视图”, “文件插入更新”, ]; 构造函数(专用路由器:路由器,专用安全服务:CralSecurityService){ } canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值

我的代码:

@Injectable()
导出类UserRouteAccessService实现CanActivate{
授权=[
"意见",,
"展望",,
“协议插入更新”,
“产品插入更新”,
“潜在客户插入更新”,
“文档视图”,
“文件插入更新”,
];
构造函数(专用路由器:路由器,专用安全服务:CralSecurityService){
}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
//布尔变量
此.securityService.securityActions().subscribe(
数据=>{
//控件是否验证正确
//如果是,则设置为真
//如果没有设置为false
},
错误=>{
//返回错误
}
)
//返回布尔值
}
}
这些评论是我想做的,但我真的不知道我应该写什么,因为这是我第一次使用angular 4

基本上,如果发送了来自authorized的一些参数,我想设置为True;如果服务中没有参数,则设置为false

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

服务:

securityActions():可观察{
返回this.http.post(
`${this.ENDPOINT}/security actions`,
无效的
);
}

您希望返回布尔值,但只能异步获取布尔值。所以不能返回布尔值。幸运的是,正如您在方法的返回类型中所看到的,您可以返回一个
可观察的
。一个可观察的(或承诺)正是用来实现这一点的:返回一个异步结果

因此,不要订阅由
securityActions()
返回的可观察对象。相反,使用
map()
catchError()
运算符将可观察对象转换为可观察对象:

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:

// 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));

您的securityService订阅返回什么?使用服务编辑所以
数据中有什么?在安全服务的成功内部,这个.route.params.subscribe(params=>console.log(params));。。。对象中的参数,所有参数都作为键/值。您可以添加逻辑并返回true/false。该服务返回一个数组,其中包含“authorized”数组中的字符串列表。它向我发送这些字符串中的一些/全部/无一个,并且基于是否有东西到达,我必须设置它是真是假
return this.securityService.securityActions().pipe(
    map(data => {
            //control if auth. is correct

            //if yes set true
            //if not set false
    },
    catchError(() => of(false))
);
// 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));