【问题标题】:typescript and sweetalert2 Uncaught ReferenceError: exports is not definedtypescript 和 sweetalert2 未捕获的 ReferenceError:未定义导出
【发布时间】:2019-02-28 06:24:53
【问题描述】:

启动了一个全新的 .net core 2.0 项目来开始学习,我选择使用和学习 typescript。 我一直在关注此处的指南:typescript guide

这可以编译并正常工作。

然后我想使用我过去使用过的 sweetalert2,我按照这些说明进行操作 sweetalert2

我在 ts 文件中创建了一个简单的 helloWorld()

import swal from 'sweetalert2'

function swalHelloWorld() {
    swal('hello world!');
}

它也在我的 www 文件夹的 js 文件中编译

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var sweetalert2_1 = require("sweetalert2");
function swalHelloWorld() {
    sweetalert2_1.default('hello world!');
}

并包含在 _layout 页面中

现在当我运行我的项目时,我得到以下错误

未捕获的 ReferenceError:未定义导出 在 app.js:2(匿名)@app.js:2

第 2 行如下

Object.defineProperty(exports, "__esModule", { value: true });

我尝试按照指南 here 进行更正,但这没有帮助

我的 tsconfig.json 是

{
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node"
  },
  "files": [
    "./scripts/app.ts"
  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "compileOnSave": true
}

我不确定如何解决此问题

webpack 配置

var path = require('path');

module.exports = {
    entry: {
        site: [
            './Scripts/app.ts']
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'wwwroot/dist/')
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
            },
        ]
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"]
    }
};

git 仓库:https://github.com/iamthebarnsley/TSExample

【问题讨论】:

    标签: javascript typescript webpack asp.net-core-2.0


    【解决方案1】:

    您的 HTML 页面似乎仍在引用 app.js。如果你想关注the guide you linked,HTML 页面应该引用 Webpack 生成的bundle.js 文件。

    第二轮

    如果你想用<input id="swalalert" type="button" value="swal alert" onclick="swalHelloWorld();" />从你的HTML调用swalHelloWorld,那么你需要全局定义swalHelloWorld

    import swal from 'sweetalert2'
    
    function swalHelloWorld() {
        swal('hello from sweet alert');
    }
    (<any>window).swalHelloWorld = swalHelloWorld;
    

    没有这个,Webpack 很聪明,意识到没有办法调用swalHelloWorld(因为它也没有从模块中导出)并从输出中省略它。如前所述,当我进行此更改并将 HTML 中的 build/app.js 替换为 dist/bundle.js 时,警报对我有效。

    2018 年 9 月 30 日更新

    我了解了一个更简洁的解决方案:将 library 选项添加到 Webpack 配置中,如 here 所示,并使用您选择的名称(例如,swalHelloWorld),这将定义一个名为 @987654334 的全局变量@ 代表整个入口点模块。那么如果你从模块中导出一个函数:

    import swal from 'sweetalert2'
    
    export function swalHelloWorld() {
        swal('hello from sweet alert');
    }
    

    HTML 将能够将其称为 swalHelloWorld.swalHelloWorld(...) 或类似名称。

    【讨论】:

    • 感谢您的信息,重新阅读指南,现在正在生成一个 bundle.js 文件,该文件正在输出到 www/dist。我将它包含在我的 _layout 页面上,但似乎我在 TS 文件中编写的 helloWorld() 没有包含在 bundle.js 文件中,所以我还错过了什么?
    • 请将您的 Webpack 配置和 bundle.js 文件的内容添加到问题中,以便我查看发生了什么。
    • 我不能包含 bundle.js,因为我会超出问题的字符数限制
    • Webpack 配置看起来很合理。确认一下,当您在文本编辑器中打开 bundle.js 时根本看不到 swalHelloWorld 函数,或者您只是在调用该函数时遇到问题?在第一种情况下,请将构建中的控制台输出添加到问题中,以便我可以获得更多提示,或者(更好地)发布存储库,以便我自己尝试构建。在第二种情况下,请将调用该函数的代码添加到问题中。
    • swalHelloWorld() 函数没有出现在 bundle.js 文件中,但在我看来,它确实从 sweetalert 导入了所需的资源。今晚晚些时候我会得到一个回购
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2018-02-20
    • 1970-01-01
    • 2017-08-19
    • 2017-09-20
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多