【发布时间】:2017-08-19 22:52:22
【问题描述】:
尝试在official handbook 之后实现模块,我收到以下错误消息:
未捕获的引用错误:未定义导出
在 app.js:2
但在我的代码中,我从未使用过 exports 这个名称。
我该如何解决这个问题?
文件
app.ts
let a = 2;
let b:number = 3;
import Person = require ('./mods/module-1');
模块-1.t
export class Person {
constructor(){
console.log('Person Class');
}
}
export default Person;
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true,
"outDir": "scripts/"
},
"exclude": [
"node_modules"
]
}
【问题讨论】:
-
您确定您输入的
exports末尾没有带有s 而不是export?这将解释错误消息,因为 s 是错误的。 -
我输入 export 而不是 exports
-
任何来自存储库的示例,它可以工作 10000%
-
这是在哪里运行的?在网页上?在 node.js 服务器上?在最终运行 javascript 的运行时环境中,您将需要一个模块加载器。从编译器标志中,您正在使用 commonjs。我对 commonjs 不是很熟悉,但是您需要先设置 commonjs,然后 Typescript 模块才能工作,或者您需要更改为另一个模块加载器(如 require.js)并进行设置。
标签: typescript module