具有嵌套数组的typescript类 - 创建模拟数组时出错

时间:2017-06-20 21:25:26

标签: arrays angular typescript typescript2.0

我正在尝试在Typescript 2.3.3(Angular 4)中定义一个模拟对象数组,但是我遇到了错误。

我的主要数据类在名为invoice-config.ts的文件中定义:

import {CustomerVariant} from './customer-variant'                              

export class InvoiceConfig {                                                    
  customerName: string;                                                         
  customerVariants: CustomerVariant[];                                          
}

这些是customer-variant.ts

的内容
export class CustomerVariant {                                                                                                         
  id: string;                                                                  
  templates: string[];                                                 
} 

现在,我想在名为InvoiceConfig的文件中创建mock-invoice-configs.ts个对象的模拟数组。我试过这个文件:

import { InvoiceConfig } from './invoice-config';                               

export const INVOICE_CONFIGS: InvoiceConfig[] = [                               

  {                                                                             
    customerName: "CUSTOMER1",                                                  
    customerVariants = [                                                        
      {                                                                                                     
        id: "A9",                                                       
        templates = [                                                  
          "default"                                                             
        ]                                                                       
      }                                                                         
    ]                                                                           
  },                                                                            

  {                                                                             
    customerName: "CUSTOMER2",                                                        
    customerVariants = [                                                        
      {                                                                                                            
        id: "A3",                                                       
        templates = [                                                  
          "default"                                                             
        ]                                                                       
      }                                                                         
    ]                                                                           
  }
]     

但它会产生错误:

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (7,5): Cannot find name 'customerVariants'.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (7,22): '=' can only be used in an object literal property inside a destructuring assignment.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (19,5): Cannot find name 'customerVariants'.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (19,22): '=' can only be used in an object literal property inside a destructuring assignment.

我不明白为什么它找不到'customerVariants'(是InvoiceConfig类的属性之一?)。 如何在不使用'='?

的情况下定义嵌套对象数组(customerVariants)

1 个答案:

答案 0 :(得分:2)

您需要将=替换为: E.g。

export const INVOICE_CONFIGS: InvoiceConfig[] = [ { 
    customerName: "CUSTOMER1", 
    customerVariants: [ { id: "A9", templates: [ "default" ] } ] }
]