如何解决rxjs订阅中的“属性为只读”
我对数组有问题:
当我尝试在countBrends()函数中编辑“ brands.count”时,出现错误 “ count是只读的” 。
有什么建议吗?
代码:
allCars:Car[]
brands:BrandCB[]
types:TypeCB[]
arr:any[] = [{count:1},{count:2},{count:3}]
constructor( private store:Store<StoreState>){ }
ngOnInit(): void {
this.store.dispatch(carsActions.getAllCars())
this.store.dispatch(brandsActions.getBrands())
this.store.pipe(select(carsSelectors.getAllCars)).subscribe(
cars => {
this.allCars = [...cars]
}
)
this.store.pipe(select(brandsSelectors.getBrands)).subscribe(
brands => {
this.brands = this.countBrands([...brands])
}
)
}
// it counts how many cars there are of the same brand
// and enters the number in the "count" property of brands
countBrands(brands):BrandCB[]{
return brands.forEach( brand =>{
let count = 0
this.allCars.forEach( car => {
car.brand == brand.name ? count++ : null
})
brand.count = count
})
}
解决方法
无论如何都应该避免突变。在这种情况下,我认为您应该用增加的数量来分配品牌的新价值,因为所有状态都是不可变的,您不能只是在组件中分配新价值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。