【问题标题】:How to use a class inside a named module that is in seperate file? Throws ReferenceError: module is not defined如何在单独文件中的命名模块中使用类?引发 ReferenceError: 模块未定义
【发布时间】:2016-02-27 16:56:37
【问题描述】:

我正在尝试使用 TypeScript ECMAScript6 使用 Visual Studio 2015 社区版创建一个简单的 Node.js 控制台应用程序,但无法使用 app.ts 中模块内定义的类。然而,Visual Studio 确实将模块“DataModels”显示为命名空间以及 intellisense 中的类,但是在 app.ts 中初始化它时会引发错误

错误:ReferenceError:未定义数据模型

尝试使用 AMD 和 CommonJs 作为模块系统的 VS 项目设置,但没有成功。

文件夹结构

/
 app.ts
 DataModels.ts
 Scripts
  Typings (dir)
   Node   (dir)
    node.d.ts

app.ts

/// <reference path="DataModels.ts" />
var user: IUser = new DataModels.User();
user.Name = 'user1';
console.log(user.Name);

DataModels.ts

interface IUser {
    Name: string;
    Email: string;
    UserName: string;
    Password: string;
    ProfilePicPath: URL;

}

module DataModels {

    export class User implements IUser {
        private _name: string;
        private _email: string;
        private _username: string;
        private _password: string;
        private _profilePicPath: URL;

        public get Name() {
            return this._name;
        }
        public set Name(value) {
            this._name = value;
        }

        public get Email() {
            return this._email;
        }
        public set Email(value) {
            this._email = value;
        }

        public get UserName() {
            return this._username;
        }
        public set UserName(value) {
            this._username = value;
        }

        public get Password() {
            return this._password;
        }
        public set Password(value) {
            this._password = value;
        }

        public get ProfilePicPath() {
            return this._profilePicPath;
        }
        public set ProfilePicPath(value) {
            this._profilePicPath = value;
        }
    }
}

【问题讨论】:

    标签: node.js typescript visual-studio-2015 ecmascript-6


    【解决方案1】:

    尝试使用 AMD 和 CommonJs 作为模块系统的 VS 项目设置,但没有成功。

    您的代码不会与任何模块系统一起使用,因为它不是以外部模块格式编写的,它只有在您将项目编译成单个文件时才能工作。现在,假设您确实想使用某种模块系统,那么您应该如何编写代码以使用 AMD/CommonJS 等:

    app.ts

    // note the lack of reference paths
    import * as DataModels from './DataModels';
    
    var user: DataModels.IUser = new DataModels.User();
    user.Name = 'user1';
    console.log(user.Name);
    

    DataModels.ts

    export interface IUser {
      ...
    }
    
    export class User implements IUser {
      ...
    }
    

    【讨论】:

    • import * 给出了运行时错误 ",CnsoleApp2\DataModels.js:1 (function (exports, require, module, __filename, __dirname) { class User { SyntaxError: Block-scoped declarations (let, const, function, class) 在严格模式之外尚不支持。关键字“class”已突出显示。
    • // 不能使用模块名称导入为.. // 而是使用如下的新名称: import * as ModuleDataModels from './DataModels'; var user: ModuleDataModels.IUser = new ModuleDataModels.User();用户名 = 'user1'; console.log(user.Name);
    • "use strict"; 放在源文件的顶部,这是在 TypeScript 1.8 中自动完成的,所以我猜你使用的是旧版本。
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2015-05-17
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多