未捕获的 ReferenceError:未定义导出并且需要 [英] Uncaught ReferenceError: exports is not defined and require
问题描述
我正在使用 angularjs 和 typescript 创建一些应用程序,我遇到了无法解决的错误问题
I'm using angularjs and typescript to create some app, I'm having a trouble with this error that I can't solve
这是我的 *.ts 代码
Here is my *.ts code
export var NgApp = new application.Startup();
///<reference path="../../../../../typings/tsd.d.ts"/>
import {NgApp} from "../../bootstrap";
module views.components.home {
export class HomeComponent {
public constructor() {
}
public static factory():Function[] {
return [
() => new HomeComponent()
]
}
}
}
NgApp.registerComponents(views.components.home,() => this.app.controller);
这是我的 GULP 任务
Here is my GULP task
let tsResult = gulp.src('src/**/*.ts').pipe(ts({
module: 'commonjs',
sourceMap: true
}));
let taskTs = tsResult.js.pipe(gulp.dest('built/'));
这是我的错误:未捕获的 ReferenceError:未定义导出
This is my error: Uncaught ReferenceError: exports is not defined
问题是:如何在打字稿中使用像 es6 这样的导入?我错过了什么?
The question is: How can I use import like es6 in typescript? What I am missing?
推荐答案
我使用 angular 1.6 和 webpack,当我将模块更改为 "umd"
I use angular 1.6 and webpack, it works on my side when i changed the module to "umd"
UMD:通用模块定义支持两种样式(AMD 和 CommonJS)的通用"模式
UMD: Universal Module Definition This has brought about the push for a "universal" pattern that supports both styles (AMD and CommonJS)
参考:http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
这里粘贴的是我更改后的 tsconfig.json,我运行 $ tsc
pasted here is my tsconfig.json after I changed it, I run $ tsc
{
"compilerOptions": {
"target": "es5",
"module": "umd",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"allowUnreachableCode": true
},
"exclude": [
"node_modules"
]
}
然后我的exports is not defined"错误消失了.
这篇关于未捕获的 ReferenceError:未定义导出并且需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!