我遇到TypeScript错误:
类型'(data:Products)=> void'的参数不能分配给类型'(value:Products [])=> void'的参数。 参数“数据”和“值”的类型不兼容。 类型“产品[]”不可分配给类型“产品”。 类型'Products []'中缺少属性'id'。
这是我的模式:
export class Products {
id: number;
prodImage: any;
catid: number;
catId: number;
}
这是我得到错误的ts代码:
import { Component, OnInit } from '@angular/core';
import { Router, Params, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {Products } from '../Admin/products/products';
import {ProductsService } from '../Admin/products/products.service';
@Component({
selector: 'app-cart',
templateUrl: './cart.component.html',
styleUrls: ['./cart.component.css']
})
export class CartComponent implements OnInit {
sess_id: any;
prods: Products;
cartList: Products;
constructor(
private router: Router,
private productsService: ProductsService,
private routes: ActivatedRoute,
) { }
ngOnInit() {
this.sess_id = localStorage.getItem('sess_key');
this.productsService.viewCart(this.sess_id)
.subscribe((data: Products) => { //getting error over here
this.cartList = data;
console.log(this.cartList);
});}}
我要去哪里错了?预先感谢
答案 0 :(得分:1)
您正在从服务而不是Products元素中获取Products数组。将cardList设置为:
cartList :Products[]