簡體   English   中英

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

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

我開始使用Angular並遇到錯誤。

... / mocks.ts(9,4):輸入'{“id”:number; “name”:string; “description”:字符串; “inStock”:數字; “價格”:數量; “featu ...”不能分配給'CarPart []類型。對象文字只能指定已知屬性,'carPart'類型中不存在'“featured”'。

我的代碼:

汽車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中刪除“精選”時它很好,沒有錯誤。 如果我添加它或任何其他名稱或類型,它將無法正常工作......有人能解釋一下嗎?

我重新啟動了Angular CLI服務器( Ctrl + C > ng服務)。 編譯沒有問題......

證據表明“你試過把它關掉再打開”仍然是有意義的。 感謝時間看Tim和Max的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM