gpt4 book ai didi

Angular Object literal 可能只指定已知属性并且......在类型中不存在

转载 作者:太空狗 更新时间:2023-10-29 17:03:47 25 4
gpt4 key购买 nike

我刚开始使用 Angular 时遇到了错误。

.../mocks.ts(9,4): Type '{"id": number; "name": string; "description": string; "inStock": number; "price": number; "featu...' is not assignable to type 'CarPart[]. Object literal may only specify known properties, and '"featured"' does not exist in type 'CarPart'.

我的代码:

汽车零件.ts

export class CarPart {
id: number;
name: string;
description: string;
inStock: number;
price: number;
featured: boolean;
}

模拟.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
}];

汽车零件.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 中删除“featured”时,没问题,没有错误。如果我添加它或任何其他名称或类型,它将不起作用......谁能解释一下?

最佳答案

我重新启动了 Angular CLI 服务器(Ctrl+C> ng serve)。编译没有问题...

“您是否尝试过将其关闭并重新打开”的证据仍然具有相关性。感谢 Tim 和 Max 花时间查看问题

关于Angular Object literal 可能只指定已知属性并且......在类型中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com