Javascript ReferenceError:在同一文件中定义多LPlie类时未定义对象
我使用node.js7.44 在文件user.models.ts中,我有如下多个LPlie类:Javascript ReferenceError:在同一文件中定义多LPlie类时未定义对象,javascript,node.js,typescript,class-transformer,Javascript,Node.js,Typescript,Class Transformer,我使用node.js7.44 在文件user.models.ts中,我有如下多个LPlie类: import { Exclude, Expose } from "class-transformer"; export class User { @Expose() _id: string; @Expose() fname: string; @Expose() lname: string; @Expose() birthday: Date; @Expos
import { Exclude, Expose } from "class-transformer";
export class User {
@Expose() _id: string;
@Expose() fname: string;
@Expose() lname: string;
@Expose() birthday: Date;
@Expose() address: Address;
constructor(){...}
}
@Exclude()
export class Address {
@Expose() street: string;
@Expose() num: number;
@Expose() city: string;
@Expose() zipCode: number;
}
当我编译服务器时,我得到一个错误ReferenceError:address未定义
如何在同一个文件中使用多个LPlie类?由于您在
用户
中使用地址
,因此必须先定义地址
。考虑使用EsLink规则。
首先定义类,然后使用它。因为提升的概念不适用于类。注意:我认为您应该每个文件只导出一个对象,请参见这里的答案:Shubham thx它真的有效!
import { Exclude, Expose } from "class-transformer";
@Exclude()
export class Address {
@Expose() street: string;
@Expose() num: number;
@Expose() city: string;
@Expose() zipCode: number;
}
export class User {
@Expose() _id: string;
@Expose() fname: string;
@Expose() lname: string;
@Expose() birthday: Date;
@Expose() address: Address;
constructor(){...}
}