首頁 > web前端 > js教程 > 主體

淺談Angular取消訂閱

小云云
發布: 2018-01-16 09:02:49
原創
1411 人瀏覽過

本文主要介紹了淺談Angular 中何時取消訂閱,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

你可能知道當你訂閱 Observable 物件或設定事件監聽時,在某個時間點,你需要執行取消訂閱操作,進而釋放作業系統的記憶體。否則,你的應用程式可能會出現記憶體洩漏。

接下來讓我們看一下,需要在 ngOnDestroy 生命週期鉤子中,手動執行取消訂閱操作的一些常見場景。

手動釋放資源場景

表單


#
export class TestComponent {

 ngOnInit() {
  this.form = new FormGroup({...});
  // 监听表单值的变化
  this.valueChanges = this.form.valueChanges.subscribe(console.log);
  // 监听表单状态的变化              
  this.statusChanges = this.form.statusChanges.subscribe(console.log);
 }

 ngOnDestroy() {
  this.valueChanges.unsubscribe();
  this.statusChanges.unsubscribe();
 }
}
登入後複製

以上方案也適用於其它的表單控制項。

路由


export class TestComponent {
 constructor(private route: ActivatedRoute, private router: Router) { }

 ngOnInit() {
  this.route.params.subscribe(console.log);
  this.route.queryParams.subscribe(console.log);
  this.route.fragment.subscribe(console.log);
  this.route.data.subscribe(console.log);
  this.route.url.subscribe(console.log);
  
  this.router.events.subscribe(console.log);
 }

 ngOnDestroy() {
  // 手动执行取消订阅的操作
 }
}
登入後複製

Renderer 服務


export class TestComponent {
 constructor(
  private renderer: Renderer2, 
  private element : ElementRef) { }

 ngOnInit() {
  this.click = this.renderer
    .listen(this.element.nativeElement, "click", handler);
 }

 ngOnDestroy() {
  this.click.unsubscribe();
 }
}
登入後複製

Infinite Observables

當你使用interval() 或fromEvent() 運算元時,你建立的是一個無限的Observable 物件。這樣的話,當我們不再需要使用它們的時候,就需要取消訂閱,手動釋放資源。


export class TestComponent {
 constructor(private element : ElementRef) { }

 interval: Subscription;
 click: Subscription;

 ngOnInit() {
  this.interval = Observable.interval(1000).subscribe(console.log);
  this.click = Observable.fromEvent(this.element.nativeElement, 'click')
              .subscribe(console.log);
 }

 ngOnDestroy() {
  this.interval.unsubscribe();
  this.click.unsubscribe();
 }
}
登入後複製

Redux Store


#
export class TestComponent {

 constructor(private store: Store) { }

 todos: Subscription;

 ngOnInit() {
   /**
   * select(key : string) {
   *  return this.map(state => state[key]).distinctUntilChanged();
   * }
   */
   this.todos = this.store.select('todos').subscribe(console.log); 
 }

 ngOnDestroy() {
  this.todos.unsubscribe();
 }
}
登入後複製

無需手動釋放資源場景

AsyncPipe


@Component({
 selector: 'test',
 template: ``
})
export class TestComponent {
 constructor(private store: Store) { }
 
 ngOnInit() {
   this.todos$ = this.store.select('todos');
 }
}
登入後複製

當元件銷毀時,async 管道會自動執行取消訂閱操作,進而避免記憶體洩漏的風險。

Angular AsyncPipe 原始碼片段


@Pipe({name: 'async', pure: false})
export class AsyncPipe implements OnDestroy, PipeTransform {
 // ...
 constructor(private _ref: ChangeDetectorRef) {}

 ngOnDestroy(): void {
  if (this._subscription) {
   this._dispose();
  }
 }
}
登入後複製

@HostListener


export class TestDirective {
 @HostListener('click')
 onClick() {
  ....
 }
}
登入後複製

需要注意的是,如果使用@HostListener 裝飾器,新增事件監聽時,我們無法手動取消訂閱。如果需要手動移除事件監聽的話,可以使用以下的方式:


// subscribe
this.handler = this.renderer.listen('document', "click", event =>{...});

// unsubscribe
this.handler();
登入後複製

Finite Observable

當你使用HTTP 服務或timer Observable 物件時,你也不需要手動執行取消訂閱操作。


export class TestComponent {
 constructor(private http: Http) { }

 ngOnInit() {
  // 表示1s后发出值,然后就结束了
  Observable.timer(1000).subscribe(console.log);
  this.http.get('http://api.com').subscribe(console.log);
 }
}
登入後複製

timer 運算子

運算子簽章

複製程式碼 程式碼如下:


public static timer(initialDelay: number | Date, period: number, scheduler: Scheduler): Observable

##運算子作用


timer 返回一個發出無限自增數列的Observable,有一定的時間間隔,這個間隔由你來選擇。

運算子範例



// 每隔1秒发出自增的数字,3秒后开始发送
var numbers = Rx.Observable.timer(3000, 1000);
numbers.subscribe(x => console.log(x));

// 5秒后发出一个数字
var numbers = Rx.Observable.timer(5000);
numbers.subscribe(x => console.log(x));
登入後複製

最終建議


你應該盡可能少的呼叫unsubscribe() 方法,你可以在RxJS: Don't Unsubscribe 這篇文章中了解與Subject 相關更多資訊。

具體範例如下:


export class TestComponent {
 constructor(private store: Store) { }

 private componetDestroyed: Subject = new Subject();
 todos: Subscription;
 posts: Subscription;

 ngOnInit() {
   this.todos = this.store.select('todos')
           .takeUntil(this.componetDestroyed).subscribe(console.log); 
           
   this.posts = this.store.select('posts')
           .takeUntil(this.componetDestroyed).subscribe(console.log); 
 }

 ngOnDestroy() {
  this.componetDestroyed.next();
  this.componetDestroyed.unsubscribe();
 }
}
登入後複製

takeUntil 運算子

運算子簽章



public takeUntil(notifier: Observable): Observable
登入後複製

運算子作用


發出來源Observable 發出的值,直到notifier Observable 發出值。

運算元範例



var interval = Rx.Observable.interval(1000);
var clicks = Rx.Observable.fromEvent(document, 'click');
var result = interval.takeUntil(clicks);

result.subscribe(x => console.log(x));
登入後複製
相關推薦:


node.js 發佈訂閱模式的方法

JavaScript發布訂閱模式用法詳

#PHP微信公眾平台開發 訂閱事件處理_PHP教學

##PHP微信公眾平台開發 訂閱事件處理_PHP教學#######

以上是淺談Angular取消訂閱的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!