我开始使用Angular并遇到错误。
... / mocks.ts(9,4):输入' {" id":number; " name":string; " description":string; " inStock":数字; "价格":数字; " featu ...'不能分配给' CarPart []。 对象文字只能指定已知属性,并且""特色"'类型' CarPart'中不存在。
我的代码:
轿厢part.ts
export class CarPart {
id: number;
name: string;
description: string;
inStock: number;
price: number;
featured: boolean;
}
mock.ts
import { CarPart } from './car-part';
export const CARPARTS: CarPart[] = [{
"id": 1,
"name": "Super Tires",
"description": "These tires are the very best",
"inStock": 5,
"price": 299.99,
"featured": false //Line 9
},
{
"id": 2,
"name": "Reinforced Shocks",
"description": "Shocks made of kryptonite",
"inStock": 4,
"price": 500.50,
"featured": false
},
{
"id": 3,
"name": "Padded Seats",
"description": "Super soft seats for a smooth ride",
"inStock": 0,
"price": 333.33,
"featured": true
}];
轿厢parts.component.ts
import { Component, OnInit } from '@angular/core';
import { CarPart } from './car-part';
import { CARPARTS } from './mock';
@Component({
selector: 'car-parts',
templateUrl: './car-parts.component.html',
styleUrls: ['./car-parts.component.css']
})
export class CarPartsComponent implements OnInit {
constructor() { }
carParts: CarPart[];
totalCarParts(){
let sum = 0;
for(let carPart of this.carParts){
sum += carPart.inStock;
}
return sum;
}
ngOnInit() {
this.carParts = CARPARTS;
}
}
当我删除"特色"来自mock.ts和car-part.ts它很好,没有错误。如果我添加它或任何其他名称或类型,它将无法正常工作...... 有人可以解释一下吗?
答案 0 :(得分:35)
我重新启动了Angular CLI服务器( Ctrl + C > ng服务)。 编译没有问题...
有证据表明"你是否尝试过将其关闭再打开"仍然相关。 感谢您及时了解Tim和Max的问题