Angular 如何取消订阅制作切换图的可观察对象

Angular 如何取消订阅制作切换图的可观察对象,angular,rxjs,Angular,Rxjs,下面的代码允许我向服务器发出http请求,以便在几个可能的(池数据)中具有给定的状态 getIntervalStatus(sessionId:string):可观察{ 返回间隔(1000)。管道( 开关映射(值=>{ 返回此.getStatus(sessionId); }), ); } 如何取消订阅由interval(1000)返回的可观察内容 当我取消订阅getIntervalStatus.subscribe()时,它不会停止继续传输的interval(1000)。见下面的例子 this.g

下面的代码允许我向服务器发出http请求,以便在几个可能的(池数据)中具有给定的状态

getIntervalStatus(sessionId:string):可观察{
返回间隔(1000)。管道(
开关映射(值=>{
返回此.getStatus(sessionId);
}),
);
}
如何取消订阅由
interval(1000)
返回的可观察内容

当我取消订阅
getIntervalStatus.subscribe()
时,它不会停止继续传输的
interval(1000)
。见下面的例子

this.getIntervalStatusSubscription=this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscription(
状态=>{
开关(此状态){
“已打开”案例:
打破
“已付款”案例:
this.getIntervalStatusSubscription.unsubscribe();
打破
案件“结案”:
this.getIntervalStatusSubscription.unsubscribe();
打破
“已使用”案例:
this.getIntervalStatusSubscription.unsubscribe();
打破
违约:
}
},
在满足特定条件之前,如何使用保持连接打开

//当状态未“打开”时,此可观察的自动取消订阅
状态$=this.device.getIntervalStatus()管道(
takeWhile(状态=>状态===‘已打开’)
);
在满足特定条件之前,如何使用保持连接打开

//当状态未“打开”时,此可观察的自动取消订阅
状态$=this.device.getIntervalStatus()管道(
takeWhile(状态=>状态===‘已打开’)
);

我认为您所面临的问题是由于您的
开关
语句造成的。 在
开关
语句中使用
此.status
,而不是
getIntervalStatus
中的status。请尝试:

this.getIntervalStatusSubscription = this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscribe(
             status => {
              switch (status) {//use the status from the subscribe method
                case 'opened':
                  break;
                case 'payed':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                case 'closed':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                case 'used':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                default:
              }
            },

您还可以简化订阅方法,您希望取消订阅每个状态,除了
“已打开”
。因此您可以编写:

this.getIntervalStatusSubscription = this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscribe(
             status => {
              if(status !== 'opened'){
                this.getIntervalStatusSubscription.unsubscribe();
              }
             }
            },

我认为您面临的问题是由于您的
开关
语句引起的。 在
开关
语句中使用
此.status
,而不是
getIntervalStatus
中的status。请尝试:

this.getIntervalStatusSubscription = this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscribe(
             status => {
              switch (status) {//use the status from the subscribe method
                case 'opened':
                  break;
                case 'payed':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                case 'closed':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                case 'used':
                  this.getIntervalStatusSubscription.unsubscribe();
                  break;
                default:
              }
            },

您还可以简化订阅方法,您希望取消订阅每个状态,除了
“已打开”
。因此您可以编写:

this.getIntervalStatusSubscription = this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscribe(
             status => {
              if(status !== 'opened'){
                this.getIntervalStatusSubscription.unsubscribe();
              }
             }
            },

在订阅此可观察工具的文件中,请执行以下操作: 1.从类型BehaviorSubject创建变量,例如

isDestroyed: BehaviorSubject<false>
  • 然后在onDestroy组件生命周期发射值中:

  • 在订阅此可观察工具的文件中,请执行以下操作: 1.从类型BehaviorSubject创建变量,例如

    isDestroyed: BehaviorSubject<false>
    
  • 然后在onDestroy组件生命周期发射值中: