在这里,x 无法访问 ShoppingCart 的属性,它显示的错误是 Property item does not exist on type {} 我不知道我犯的错误在哪里,我无法识别
购物车.service.ts
async getCart(): Promise<Observable<ShoppingCart>> {
let cartId = await this.getOrCreateCartId();
return this.db.object('/shopping-carts/' + cartId)
.valueChanges()
.pipe(
map(x => new ShoppingCart(x.items))
);
}
**ShoppingCart.ts**
import { Product } from './product';
import { ShoppingCartItem } from "./shopping-cart-item";
export class ShoppingCart {
items: ShoppingCartItem[] = [];
constructor(private itemsMap: { [productId: string]: ShoppingCartItem }) {
this.itemsMap = itemsMap || {};
for (let productId in itemsMap) {
//we explicitly map each of the object to shoppingCart object
let item = itemsMap[productId];
this.items.push(new ShoppingCartItem({
// title: item.title,
// imageUrl: item.imageUrl,
// price: item.price,
...item,
key: productId
}));
}
}