简体   繁体   English

Angular Object文字只能指定知道属性,而...在类型中不存在

[英]Angular Object literal may only specify know properties and … does not exist in type

I'm starting out with Angular and running into an error. 我开始使用Angular并遇到错误。

.../mocks.ts(9,4): Type '{"id": number; ... / mocks.ts(9,4):输入'{“id”:number; "name": string; “name”:string; "description": string; “description”:字符串; "inStock": number; “inStock”:数字; "price": number; “价格”:数量; "featu...' is not assignable to type 'CarPart[]. Object literal may only specify known properties, and '"featured"' does not exist in type 'CarPart'. “featu ...”不能分配给'CarPart []类型。对象文字只能指定已知属性,'carPart'类型中不存在'“featured”'。

My code: 我的代码:

car-part.ts 汽车part.ts

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

mock.ts 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
  }];

car-parts.component.ts 汽车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;
  }

}

when I remove "featured" from both mock.ts and car-part.ts it is fine, no error. 当我从mock.ts和car-part.ts中删除“精选”时它很好,没有错误。 If I add it or any other name or type, it wont work... Can someone explain this? 如果我添加它或任何其他名称或类型,它将无法正常工作......有人能解释一下吗?

I restarted the Angular CLI server ( Ctrl + C > ng serve). 我重新启动了Angular CLI服务器( Ctrl + C > ng服务)。 Compiled without problems... 编译没有问题......

Evidence that "Have you tried turning it off and on again" is still relevant. 证据表明“你试过把它关掉再打开”仍然是有意义的。 Thanks for the time looking at the problems Tim and Max 感谢时间看Tim和Max的问题

暂无
暂无

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

相关问题 对象字面量只能指定已知的属性,并且类型“设置”中不存在“选择” - Object literal may only specify known properties, and 'select' does not exist in type 'Settings' 对象文字只能指定已知的属性,并且'ChatMessage []'类型中不存在'message' - Object literal may only specify known properties and 'message' does not exist in type 'ChatMessage[]' 错误TS2322:对象文字可能仅指定已知属性,并且类型中不存在“标签” - error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type Object 文字可能只指定已知属性,并且“设置”类型中不存在“按钮” - Object literal may only specify known properties, and 'buttons' does not exist in type 'Settings 错误:对象字面量只能以角度指定已知属性 - error: Object literal may only specify known properties in angular Object 文字只能指定已知属性 Angular/TypeScript - Object literal may only specify known properties Angular/TypeScript 对象文字只能指定已知属性 - Object Literal May Only Specify Known Properties 类型的参数不能分配给类型的参数。对象文字只能指定已知的属性 - Argument of type is not assignable to parameter of type .Object literal may only specify known properties angular 4 typescript 验证错误对象文字可能只指定已知属性 - angular 4 typescript validation error Object literal may only specify known properties 对象文字可能只指定已知属性错误 - Object literal may only specify known properties error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM