2016-08-18 7 views
10

パフォーマンスと「角度のある方」にはどのように優れていますか?ビューには多くの非同期パイプがあります。角2:多くの非同期パイプと1つのサブスクライブ

例:

@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'); 
    } 
} 

答えて

3

角度を変更について通知されますので、| asyncパイプを使用して、より効率的です。最初の例では、バインディングはそれぞれの検出サイクルの変更をチェックされます。

+0

'changeDetection'を' ChangeDetectionStrategy.OnPush'に変更し、購読ブロックで 'changeDetector.markForCheck()'を使用しますか? – dakolech

+0

これはおそらく、非同期パイプを使用する場合と同じです。 –

0

これは大きな質問です。 私はしばしば、同じ観察可能なもの、OnInitを購読するもの、onDestroyを購読しないものに対して、複数の非同期パイプを使用するという決定に出くわしました。

関連する問題