简体   繁体   English

如何在angular2 [typescript]中定义具有复杂json结构的模型?

[英]How to define model with complex json structure in angular2 [typescript]?

I have one model with following properties 我有一个具有以下属性的模型

{
   Name:String,
   Id:Number,
   Store:{
      Name:String,
      City:String,
      State:String
   }
}

How can I define model with this structure in angular2 with typescript ? 如何在带有typescript的angular2中使用此结构定义模型?

Just define 2 interfaces with your required values. 只需使用所需的值定义2个接口即可。

export interface ParentModel {
    Name: string;
    Id: number;
    Store: ChildModel
}

export interface ChildModel {
    Name: string;
    City: string;
    State: string;
}

Depending on how you want to set it up these can be in the same file or two separate files, if you are putting them in separate files just remember to import the child into the parent. 根据您要设置的方式,这些文件可以位于同一文件中,也可以位于两个单独的文件中,如果要将它们放在单独的文件中,请记住将子级导入父级。

Hope that helps 希望能有所帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM