我正在通过Angular2 Tour of Heroes教程,我正在努力学习如何使用服务。我能够获得基本的教程,但是当我试图变得更复杂时,我的应用程序中断了,我不确定我做错了什么。
工作正常的基本模型包括一个mock-heroes对象以及一个指定每一行类型的hero.ts文件。
以下是我所指的英雄导览教程:https://angular.io/docs/ts/latest/tutorial/toh-pt4.html
hero.ts文件:
export class Hero {
id: number;
firstName: string;
lastName: string;
street: string;
suite: string;
city: string;
state: string;
zipcode: string;
}
mock-hero.ts文件:
import { Hero } from './hero';
export const HEROES: Hero[] =
[
{
"id": 101,
"firstName": "John",
"lastName": "Doe",
"street": "111 Main Street",
"suite": "Apt. 111",
"city": "Anytown",
"state": "US",
"zipcode": "55555-0000"
}
]
如果我想添加一个嵌套对象,比如帐户,我会收到错误:
对象文字只能指定已知属性,并且' accounts'不 不存在于类型' Hero'。
hero.ts文件:
export class Hero {
id: number;
firstName: string;
lastName: string;
street: string;
suite: string;
city: string;
state: string;
zipcode: string;
accounts: ????;
accountNum: string;
accountName: string;
type: string;
availBalance: number
}
mock-hero.ts文件:
import { Hero } from './hero';
export const HEROES: Hero[] =
[
{
"id": 101,
"firstName": "John",
"lastName": "Doe",
"street": "111 Main Street",
"suite": "Apt. 111",
"city": "Anytown",
"state": "US",
"zipcode": "55555-0000",
"accounts": [
{
accountNum: "012345678",
accountName: "Personal Checking",
type: "checking",
availBalance: 1000.00
}
]
}
]
所以,我知道我需要识别"帐户"但是我错过了我的分类和#34;帐户"因此我可以正确地嵌套对象。
提前致谢。
答案 0 :(得分:1)
export interface Account {
accountNum: string;
accountName: string;
type: string;
availBalance: number;
}
export class Hero {
id: number;
firstName: string;
lastName: string;
street: string;
suite: string;
city: string;
state: string;
zipcode: string;
accounts: Array<Account>;
}