我开始关注ngrxstore ,我看到了使用Angular 异步管道的便利性.同时,我不确定大规模使用Angular 异步管道是否是一个好 Select .
我举一个简单的例子.假设在同一个模板中,我需要显示从存储中检索到的对象(例如人)的不同属性.
一段模板代码可以是
<div>{{(person$ | async).name}}</div>
<div>{{(person$ | async).address}}</div>
<div>{{(person$ | async).age}}</div>
而组件类构造函数将具有
export class MyComponent {
person$: Observable<Person>;
constructor(
private store: Store<ApplicationState>
) {
this.person$ = this.store.select(stateToCurrentPersonSelector);
}
.....
.....
}
据我所知,这段代码意味着对同一个可观察对象(person$
)进行3次订阅(通过异步管道在模板中进行).
另一种方法是在MyComponent中定义1个属性(person
),并且只有1个订阅(在构造函数中)填充该属性,例如
export class MyComponent {
person: Person;
constructor(
private store: Store<ApplicationState>
) {
this.store.select(stateToCurrentPersonSelector)
.subscribe(person => this.person = person);
}
.....
.....
}
而模板使用标准属性绑定(即不使用异步管道),例如
<div>{{person.name}}</div>
<div>{{person.address}}</div>
<div>{{person.age}}</div>
Now the question
这两种方法在性能上有什么不同吗?大量使用异步管道(即大量使用订阅)会影响代码的效率吗?