[英]How can I unsubscribe from this subscription?
I have a PolicyGuard
with a method canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
.我有一个带有方法PolicyGuard
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
的 PolicyGuard。
In canActivate
I create an observable, and inside of this observable I subscribe to an Observable that I retrieve from my AbilityService
.在canActivate
,我创建了一个 observable,在这个 observable 内部,我订阅了一个从AbilityService
检索到的 Observable。
The observable I retrieve from AbilityService
listens for changes to a users "policies"/"abilities".我从AbilityService
检索到的 observable 监听用户“策略”/“能力”的变化。
This gets pretty messy when I am making changes to a user's "policies" and pushing those changes to the client using Socket.io
as page re-direction to the home page can happen when I don't want it to (One subscription runs obs.next(true)
while another subscription routes to home and runs obs.next(false)
)当我对用户的“策略”进行更改并使用Socket.io
将这些更改推送到客户端时,这会变得非常混乱,因为当我不希望它发生页面重定向到主页时(一个订阅运行obs.next(true)
而另一个订阅路由到家并运行obs.next(false)
)
Is there a way I can make the subscription unsubscribe when leaving the page that the subscription was meant for?离开订阅的页面时,有没有办法让订阅取消订阅?
@Injectable()
export class PolicyGuard implements CanActivate {
constructor(protected router: Router, private abilityService: AbilityService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
console.log("PolicyGuard() - canActivate()");
return new Observable<boolean>(obs => {
const observable = this.abilityService.getAbility();
observable.subscribe(ability => {
const can = ability.can(route.data.action, route.data.subject);
if (can) {
obs.next(true);
}
else {
this.router.navigate(['/home']);
obs.next(false);
}
});
});
}
}
You have currently created an additional Observable which is in my opinion not required.您当前创建了一个额外的 Observable,我认为这不是必需的。 You could simply return this.abilityService.getAbility()
and transform the return value with the RxJS map
operator.您可以简单地返回this.abilityService.getAbility()
并使用 RxJS map
运算符转换返回值。 This way Angular would take over and handle the subscription from your Observable.这样 Angular 将接管并处理来自 Observable 的订阅。
This would look like this:这看起来像这样:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
console.log("PolicyGuard() - canActivate()");
return this.abilityService.getAbility().pipe(
map(ability => {
const can = ability.can(route.data.action, route.data.subject);
if (!can) {
this.router.navigate(['/home']);
}
return can;
});
);
}
The example for canActivate on angular.io does something simliar, just without the map
operator.在 angular.io 上的canActivate 示例做了类似的事情,只是没有map
运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.