2017-06-20 114 views
0

我想在Typescript 2.3.3(Angular 4)中定義一個對象的模擬數組,但是出現錯誤。typescript class with nested arrays - 創建模擬數組時出錯

我的主要數據類是在一個名爲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

用途:代替它嗎?就像你對其他任何財產一樣..因爲它和任何其他財產一樣。 – toskv

回答

2

您需要將=替換爲: E.g.

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