javascript - typescript 和 sweetalert2 未捕获的 ReferenceError:未定义导出
问题描述
开始了一个全新的 .net core 2.0 项目来开始学习,我选择使用和学习 typescript。我一直在关注位于此处的指南:打字稿指南
这可以编译并正常工作。
然后我想使用我过去使用过的 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 });
我尝试按照此处的指南进行更正,但这没有帮助
我的 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
}
我不确定如何解决这个问题
网络包配置
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"]
}
};
解决方案
It looks like your HTML page is still referencing app.js
. If you wanted to follow the guide you linked, the HTML page should instead reference the bundle.js
file produced by Webpack.
Round 2
If you want to call swalHelloWorld
from your HTML with <input id="swalalert" type="button" value="swal alert" onclick="swalHelloWorld();" />
, then you need to define swalHelloWorld
globally:
import swal from 'sweetalert2'
function swalHelloWorld() {
swal('hello from sweet alert');
}
(<any>window).swalHelloWorld = swalHelloWorld;
Without this, Webpack is being clever and realizing there is no way to call swalHelloWorld
(since it is not exported from the module either) and omitting it from the output. When I make this change and also replace build/app.js
with dist/bundle.js
in the HTML as previously discussed, the alert is working for me.
Update, 2018-09-30
I learned about a cleaner solution: add the library
option to the Webpack configuration as shown here with a name of your choice (for example, swalHelloWorld
), and that will define a global variable named swalHelloWorld
representing the entire entry-point module. Then if you export a function from the module:
import swal from 'sweetalert2'
export function swalHelloWorld() {
swal('hello from sweet alert');
}
the HTML will be able to call it as swalHelloWorld.swalHelloWorld(...)
or similar.
推荐阅读
- c# - Nest Elasticsearch 通配符查询用作查询字符串,但不适用于 fluent API
- android - 如何显示目标图像的二维图像
- javascript - Unicode 6.0 + React PWA
- python - 如何删除列表中数字周围的“”,而不是字符串
- flutter - 当我删除列表中的第一个 TextFormField 时,TextFormField initialValue 获取下一个字段的值
- javascript - 动态创建一个javascript键值对象
- r - 返回分组数据帧R中最大值的相应变量
- c# - 带有变量的graphQL查询(C#):查询的正确语法是什么
- c# - CefSharp如何模拟点击返回结果
- c - C 中的 int main() 和 int main(void) 之间有真正的区别吗?