Angular 4:' Property'包括'缺少类型'用于自定义数据类型

时间:2017-04-30 21:09:09

标签: angular typescript types model bundle

我看到了错误:

/root/simutronx/simutron-app/src/app/app.component.ts (45,5): Type
'Promise<OneDSlider[]>' is not assignable to type 'OneDSlider[]'. Property
'includes' is missing in type 'Promise<OneDSlider[]>'.

这在控制台中:

/root/simutronx/simutron-app/src/app/mock-simutronx.ts (5,64): 
Type '{ id:    number; name: string; value: number; quantity: number; }[]'
is not assignable to type 'OneDSlider[]'.
Type '{ id: number; name: string; value: number; quantity: number; }' is
not assignable to type 'OneDSlider'.
Object literal may only specify known properties, and 'quantity' does not
exist in type 'OneDSlider'.  vendor.bundle.js:29679:4

创建数据服务后。型号(simutronx.ts):

export class OneDSlider {
  id: number;
  name: string;
  value: number;
  quantity: number;
}

如您所见,数据模型上有数量属性。 (在此之前,它没有包含数量属性,因此我重新启动了服务器进程。)

数据服务(simutronx.service.ts):

import { Injectable } from '@angular/core';

import { OneDSlider, TwoDSlider, Switch, Output } from './simutronx';
import { ONEDSLIDERS, TWODSLIDERS, SWITCHES, OUTPUTS } from './mock-simutronx';

@Injectable()
export class SimXService {
  getOneDSliders(): Promise<OneDSlider[]> {
    return Promise.resolve(ONEDSLIDERS);
  }
  getTwoDSliders(): Promise<TwoDSlider[]> {
    return Promise.resolve(TWODSLIDERS);
  }
  getSwitches(): Promise<Switch[]> {
    return Promise.resolve(SWITCHES);
  }
  getOutputs(): Promise<Output[]> {
    return Promise.resolve(OUTPUTS);
  }
}

模拟数据(mock-simutronx.ts):

import { UIConstructor, OneDSlider, TwoDSlider, Switch, Output } from './simutronx';

export const ONEDSLIDERS: OneDSlider[] = [
  { id: 1, name: 'Shipping Container Purchase Cost', value: 7, quantity: 9 },
  { id: 2, name: 'Container Rental Costs', value: 99, quantity: 15 }
];

export const TWODSLIDERS: TwoDSlider[] = [
  { id: 1, name: 'Available Shipping Containers', max_value: 99, min_value: 0, quantity: 3 },
  { id: 2, name: 'Purchasing Staff Available', max_value: 82, min_value: 0, quantity: 5 }
];

export const SWITCHES: Switch[] = [
  { id: 1, name: 'Min/Max', state: true }
];

export const OUTPUTS: Output[] = [
  { id: 1, name: 'Profit', value: 1000 }
];

我的app.component.ts:

import { Component } from '@angular/core';

import { UIConstructor, OneDSlider, TwoDSlider, Switch, Output } from './simutronx';
import { ONEDSLIDERS, TWODSLIDERS, SWITCHES, OUTPUTS } from './mock-simutronx';
import { SimXService } from './simutronx.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [SimXService]
})

export class AppComponent {
  title = 'SimutronX';
  onedsliders = ONEDSLIDERS;
  twodsliders = TWODSLIDERS;
  switches = SWITCHES;
  outputs = OUTPUTS;
  valuesinput = false;
  simXcreated = false;

  constructor(private simXService: SimXService) { }

  coefficients: UIConstructor = {
    id: 1,
    title: 'coefficients',
    value: 2
  }
  constraints: UIConstructor = {
    id: 1,
    title: 'constraints',
    value: 2
  }

  inputNums(): void {
    this.valuesinput = true;
  }

  createSimX(): void {
    this.simXcreated = true;
  }

  getOneDSliders(): void {
    this.onedsliders = this.simXService.getOneDSliders();
  }
  getTwoDSliders(): void {
    this.twodsliders = this.simXService.getTwoDSliders();
  }

}

我搜索过并发现了类似的问题:Angular 4 Error : Property 'includes' is missing in type '() => any'但是虽然错误信息类似,但问题的原因似乎并不相同。我还仔细检查了数据类型,看起来模型和模拟数据看起来是一样的......

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

线索出现错误。您正在尝试分配类型承诺&lt; OneDSlider []&gt; 到此行的{strong> OneDSlider <{1}}

您既不能从 SimXService 返回承诺,也不能让 AppComponent 等待承诺解析以检索该值。

this.onedsliders = this.simXService.getOneDSliders();

答案 1 :(得分:0)

尝试返回Promise时遇到了同样的错误消息:

  ERROR in /GitHub/Angular/skillwise-accounts-app/src/app/app.component.ts (22,5): Type 'Promise<Account[]>' is not assignable totype 'Account[]'.
  Property 'includes' is missing in type 'Promise<Account[]>'.

在查看ES7 Array.prototype.includes() missing in TypeScript后,似乎我的打字稿配置丢失了一些东西。 为了解决我的错误,我最终需要更新我的项目tsconfig.json文件。我将以下内容添加到compilerOptions

    "lib": ["dom", "es2017"],

希望这有帮助。