0

在这里,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
            }));

        }
    }
4

1 回答 1

1

如果您仍然有这个问题,那么这可能会有所帮助......

async getCart(): Promise<Observable<ShoppingCart>> {
const cartId = await this.getOrCreateCartId();
return this.db.object('/shopping-cart/' + cartId).snapshotChanges()
.pipe(map(x => new ShoppingCart(x.payload.exportVal().items)));
}
于 2019-03-13T20:11:26.597 回答