简体   繁体   English

“从不”类型上不存在属性“id”。 有角度的

[英]Property 'id' does not exist on type 'never'. angular

Hi I'm practicing on a small store website project this one but I'm having a problem with moving the items to the cart.嗨,我正在一个小型商店网站项目上练习这个项目,但我在将物品移动到购物车时遇到问题。

in the source code in file app/components/product-list/product-list.component.ts line 33 :在文件 app/components/product-list/product-list.component.ts 第 33 行的源代码中:

const cartIdx = cartProducts.findIndex(cart => cart.id === cartProduct.id)

when I do the same line I face the error: Property 'id' does not exist on type 'never'当我做同一行时,我面临错误:“从不”类型上不存在属性“id”

that's my code:那是我的代码:

  onSubmit(cartItem: Product){
let toCartItem: CartItems[] = [];
let alertMessage: string = '';

const itemCount = this.selectedOption;
const cartItems: CartItems[] | [] = this.ProductserveService.getCartItems();


const itemID = cartItems.findIndex(cart => cart.id === cartItem.id)
toCartItem = cartItems;

if((itemID === -1) || (cartItems.length === 0)){
  toCartItem.push(Object.assign(cartItem, {option: itemCount}));

  alertMessage = `New Item '${cartItem.name}' added to cart`;
}

this.ProductserveService.addToCart(toCartItem);

alert(alertMessage);

this.printLocalData();
return false;}

anyone know why is that ?有谁知道这是为什么?

我已经浏览了您的代码,您错过了 findIndex 中购物车的数据类型,将其const cartIdx = cartProducts.findIndex(cart: CartProduct => cart.id === cartProduct.id) from const cartIdx = cartProducts.findIndex(cart => cart.id === cartProduct.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM