【发布时间】:2017-09-28 09:50:52
【问题描述】:
我看到了错误:
/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' 但是虽然错误消息相似,但问题的原因似乎并不相同。我还仔细检查了数据类型,模型和模拟数据似乎是相同的......
我在这里错过了什么?
【问题讨论】:
标签: angular typescript types model bundle