简体   繁体   English

如何取消订阅此订阅?

[英]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运算符。

I found a quite nice solution to my problem by introducing a Subject , subscribing to router events, and comparing the state url with a router event url.通过引入Subject ,订阅路由器事件,并将 state url 与路由器事件 url 进行比较,我找到了一个很好的解决方案。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果没有“ Subscription”类型的对象,如何从Observable退订? - How to unsubscribe from Observable if there is no object of type “Subscription”? 如何在Angular中取消订阅未初始化的订阅 - How to unsubscribe from not initialized subscription in Angular 如何订阅和取消订阅可观察对象? - How can I subscribe and unsubscribe from an observable? 如何取消订阅事件订阅 - How to unsubscribe an events subscription 注销后如何取消订阅主题? 我的用户实际上是我的主题 - How must I unsubscribe from a subscription of subject after logout? My user is actually my subject 我应该在 Angular 的 flex-layout 中取消订阅 MediaObserver 订阅吗? - Should I unsubscribe from a MediaObserver subscription in flex-layout for Angular? Angular/RxJS 我什么时候应该取消订阅`Subscription` - Angular/RxJS When should I unsubscribe from `Subscription` 我是否需要通过组件中的订阅取消订阅 httpClient - Do I need to unsubscribe from httpClient with subscription in component 无法取消订阅ReplaySubject的订阅 - Unable to Unsubscribe from the subscription of ReplaySubject Angular observables - 如果没有订阅,我需要取消订阅吗? - Angular observables - Do I need unsubscribe if no subscription?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM