2016-08-18 16 views
10

Performans ve “açısal yol” için daha iyi olan nedir: görünümde çok sayıda asenkronizasyon borusu var mı yoksa Bileşen abonelikten çıkma ile bir abone mi var?Köşeli 2: birçok Async borusu bir aboneye karşı

Örnek:

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

veya açısal değişiklikler hakkında haberdar olur çünkü | async boru daha etkilidir kullanarak

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

cevap

3

. İlk örnekte, bağlamalar her bir değişim tespit döngüsünde kontrol edilir.

+0

Ancak "changeDetection" işlevini "ChangeDetectionStrategy.OnPush" olarak değiştirirseniz ve subscribe bloğunda "changeDetector.markForCheck()' işlevini kullanırsam? – dakolech

+0

O zaman muhtemelen async borusunu kullanmakla aynı şey. –

0

Bu harika bir soru. Sık sık aynı gözlemlenebilir, OnInit abone ve abonelik abonelik onDestroy için birden fazla async boru kullanma kararı geldi.