web-dev-qa-db-ja.com

Uncaught ReferenceError:エクスポートが定義されておらず、必要です

私は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/'));

これは私のエラーです:Uncaught ReferenceError:exports is not defined

問題は、typescriptでes6のようなインポートを使用するにはどうすればよいですか?私が欠けているものは何ですか?

8
IamStalker

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"
  ]
}

次に、私の "エクスポートが定義されていません"エラーがなくなりました。

12
KShewengger

ここをクリック 「最終的に解決」同じエラーが発生しましたmodule: "commonjs"を "module": "es6"に変更しました。ES5をターゲットにするとインポート/エクスポートステートメントが削除されるため、これらのツールでは削除できません未使用のエクスポート。

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

}
2
Umesh Shende

Typescriptでes6のようなインポートを使用するにはどうすればよいですか?私が欠けているものは何ですか?

モジュールローダーを使用する必要があります。これがwebpackを使用するサンプルです: https://github.com/AngularClass/angular2-webpack-starter

0
basarat