我正在尝试在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)答案 0 :(得分:2)
您需要将=
替换为:
E.g。
export const INVOICE_CONFIGS: InvoiceConfig[] = [ {
customerName: "CUSTOMER1",
customerVariants: [ { id: "A9", templates: [ "default" ] } ] }
]