【问题标题】:ReferenceError: Object is not defined when I define multilplie classes at same fileReferenceError:当我在同一个文件中定义多重类时,未定义对象
【发布时间】:2017-10-19 09:13:30
【问题描述】:

我使用 node.js 7.44。

在 user.models.ts 文件中,我有 multilplie 类,例如:

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: Adress is not defined

如何在同一个文件中使用 multilplie 类?

【问题讨论】:

  • 先定义类,然后使用。因为提升的概念不适用于类。
  • 注意:我认为您应该为每个文件仅导出一个对象,请在此处查看答案:stackoverflow.com/questions/41228221/…
  • Shubham 真的很管用!

标签: javascript node.js typescript class-transformer


【解决方案1】:

由于您在User 中使用Address,因此您必须首先定义Address。考虑使用 eslint 规则no-use-before-define

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(){...}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    相关资源
    最近更新 更多