Angular 2:许多异步管道与一个订阅

dak*_*ech 11 javascript rxjs typescript angular

什么是更好的性能和"角度方式":在视图中有许多异步管道或组件中的一个订户具有取消订阅动作onDestroy?

例:

@Component({
  template: `<div> {{ post.title }} {{ post.author.name }} {{ post.category.name }} </div>`
  ...
  })
class AppComponent {
  public post: Post;
  public postSubscription;

  ngOnInit() {
    postSubscription = someObservable.subscribe((post) => {
      this.post = post;
    })
  }

 ngOnDestroy() {
    postSubscription.unsubscribe();
 }
}
Run Code Online (Sandbox Code Playgroud)

要么

@Component({
  template: `<div> {{ postTitle | async }} {{ postAuthorName | async }} {{ postCategoryName | async }} </div>`
  ...
  })
class AppComponent {
  public postTitle: Observable<string>;
  public postAuthorName: Observable<string>;
  public postCategoryName: Observable<string>;

  ngOnInit() {
    this.postTitle = someObservable.pluck('title');
    this.postAuthorName = someObservable.pluck('author', 'name');
    this.postCategoryName = someObservable.pluck('category', 'name');
  }
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 5

使用| async管道效率更高,因为Angular会收到有关更改的通知。对于第一个示例,在每个更改检测周期检查绑定。