admin管理员组文章数量:1425743
What is better for performance and "angular way": have many async pipes in the view or one subscriber in the ponent with unsubscribe action onDestroy?
Example:
@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();
}
}
or
@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');
}
}
What is better for performance and "angular way": have many async pipes in the view or one subscriber in the ponent with unsubscribe action onDestroy?
Example:
@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();
}
}
or
@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');
}
}
Share
Improve this question
edited Aug 18, 2016 at 9:57
Günter Zöchbauer
659k234 gold badges2.1k silver badges1.6k bronze badges
asked Aug 18, 2016 at 9:56
dakolechdakolech
5224 silver badges17 bronze badges
0
2 Answers
Reset to default 5Using the | async
pipe is more efficient because Angular gets notified about changes. With the first example the bindings are checked each change detection cycle.
This is a great question. I often came across the decision of use multiple async pipes for the same observable, vs subscribing OnInit and unsubscribing onDestroy.
本文标签: javascriptAngular 2 many Async pipes vs one subscribeStack Overflow
版权声明:本文标题:javascript - Angular 2: many Async pipes vs one subscribe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745399302a2656962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论