【问题标题】:Import class from another file in TypeScript从 TypeScript 中的另一个文件导入类
【发布时间】:2014-09-05 05:32:56
【问题描述】:

我的问题是我想在我的 app.ts 文件中使用我的 Appointment 类。

我在顶部的 app.ts 中引用了它:

/// <reference path="Appointment.ts"/>

然后我实例化它并打印clientID:

var app : Appointment = new Appointment(1, 2, "User", 3);;

console.log(app.ClientID);

然后我得到ReferenceError: Appointment is not defined

我还将 Appointment 的 .js 脚本添加到我的 html 文件中。

我的约会如下所示:

class Appointment {
    // some properties

    constructor(clientID: number, state: number, userCreated: string, activityID: number) {
        // constructor
    }
}

【问题讨论】:

  • 查看生成的js文件,看Appointment类是否编译成javascript。如果不是检查你的类中的编译错误。
  • 是的,它编译成 Appointment.js 文件。
  • 尝试在 Appointment.ts 文件中编写此代码。如果可行,那么正如您所说,这应该是参考路径问题(检查路径)。如果没有,那就太奇怪了,我需要更多信息来帮助你..
  • app.js 之前是否加载了约会.js?
  • 如果我将约会复制到我的 app.ts 文件中,它就可以工作!所以这是一个参考错误。如何正确引用?

标签: javascript html typescript


【解决方案1】:

您的脚本引用应该如下所示(顺序很重要....js 文件扩展名也很重要!)

<script src="Appointment.js"></script>
<script src="app.js"></script>

【讨论】:

  • 天哪。不敢相信我做错了! :D 谢谢!!
猜你喜欢
  • 2016-10-21
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多