10
性能和「角度方式」更好:在視圖中有多個異步管道,或者組件中有一個訂閱者使用onDestroy取消訂閱操作?Angular 2:很多異步管道和一個訂閱
實施例:
@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();
}
}
或
@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');
}
}
但是,如果我修改'changeDetection'到'ChangeDetectionStrategy.OnPush'並在訂閱塊中使用'changeDetector.markForCheck()'? – dakolech
然後它可能與使用異步管道相同。 –