小编dak*_*ech的帖子

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

什么是更好的性能和"角度方式":在视图中有许多异步管道或组件中的一个订户具有取消订阅动作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 = …
Run Code Online (Sandbox Code Playgroud)

javascript rxjs typescript angular

11
推荐指数
1
解决办法
1573
查看次数

标签 统计

angular ×1

javascript ×1

rxjs ×1

typescript ×1