未捕获的ReferenceError:未定义导出和require

时间:2016-01-20 09:09:44

标签: angularjs typescript gulp ecmascript-6

我正在使用angularjs和typescript来创建一些应用程序,我遇到了这个我无法解决的错误的问题

这是我的* .ts代码

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任务

let tsResult = gulp.src('src/**/*.ts').pipe(ts({
    module: 'commonjs',
    sourceMap: true
}));

let taskTs = tsResult.js.pipe(gulp.dest('built/'));

这是我的错误: 未捕获的ReferenceError:未定义导出

问题是:如何在打字稿中使用像es6这样的导入?我缺少什么?

3 个答案:

答案 0 :(得分:11)

我使用angular 1.6和webpack, 当我将模块更改为“umd”

时,它可以在我这边工作
  

UMD:通用模块定义这带来了推动   支持两种风格的“通用”模式(AMD和CommonJS)

     

参考:http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/

在我更改之后,

粘贴在我的tsconfig.json中,我运行$ 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"
  ]
}

然后我的“导出未定义”错误消失。

答案 1 :(得分:2)

click here&#34;最后解决了#34;我也得到了相同的错误我改变了模块:&#34; commonjs&#34;到&#34;模块&#34;:&#34; es6&#34;,因为定位ES5会删除导入/导出语句,因此这些工具无法删除未使用的导出。

{
    "compilerOptions": {
    "target": "es5",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
    }

}

答案 2 :(得分:0)

  

如何在打字稿中使用像es6这样的导入?我缺少什么?

您需要使用模块加载器。以下是使用webpack的示例:https://github.com/AngularClass/angular2-webpack-starter

相关问题