ionic-angular中的IntervalObservable怎么停止
时间: 2024-05-13 10:18:37 浏览: 19
在 Ionic-Angular 中,您可以通过调用 `unsubscribe()` 方法来停止 IntervalObservable。例如:
```typescript
import { Component, OnInit, OnDestroy } from '@angular/core';
import { IntervalObservable } from 'rxjs/observable/IntervalObservable';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'app-my-component',
template: '<p>{{counter}}</p>'
})
export class MyComponent implements OnInit, OnDestroy {
counter: number = 0;
subscription: Subscription;
ngOnInit() {
this.subscription = IntervalObservable.create(1000).subscribe(() => {
this.counter++;
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
```
在上面的代码中,我们在 `ngOnInit()` 生命周期钩子函数中订阅了一个 IntervalObservable,并将其返回的 Subscription 对象保存到了 `subscription` 属性中。然后,在 `ngOnDestroy()` 生命周期钩子函数中,我们调用了 `unsubscribe()` 方法来停止 IntervalObservable。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)