![](/img/trans.png)
[英]Angular: How do I Rxjs filter by property through an array of objects?
[英]“property is read only” in angular with rxjs subscription
我有一个数组问题:
当我尝试在 countBrends() 函数中编辑“brands.count”时,出现错误“count is read only” 。
你有什么建议吗?
代码:
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
})
}
无论如何,您应该在可能的情况下避免突变。 在这种情况下,我认为您应该通过增加数量来分配品牌的新价值,因为所有状态都是不可变的,您不能只在组件中分配新价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.