typescript class with nested arrays - 创建模拟数组时出错
问题描述:
我想在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)?
答
您需要将=
替换为:
E.g.
export const INVOICE_CONFIGS: InvoiceConfig[] = [ {
customerName: "CUSTOMER1",
customerVariants: [ { id: "A9", templates: [ "default" ] } ] }
]
用途:代替它吗?就像你对其他任何财产一样..因为它和任何其他财产一样。 – toskv