- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用https://github.com/timmywil/panzoom来自使用 webpack 和 babel 编译的 typescript 项目。
问题在于 typescript 方法调用:
import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));
被转译为以下 JavaScript:
panzoom_1.default(document.querySelector("#pic"));
然后生成以下运行时错误:
Uncaught TypeError: panzoom_1.default is not a function
如果我调试 JavaScript,则 panzoom_1
具有预期的函数签名,并且没有 default
成员。
这是无数不同类型的模块、默认导出以及 babel 和 typescript 导入它们的方式差异之间的某种类型的问题,但我完全迷失了。根据文档,panzoom
是一个 UMD 模块(如果有帮助的话)。
我找到了一种解决方法,可以以不同的方式导入,然后将其强制转换为任何,但这显然很疯狂,对吧?:
import * as Panzoom from '@panzoom/panzoom';
(<any>Panzoom)(document.querySelector("#pic"));
这是项目配置:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<img src="pic.jpg" id="pic" />
</body>
<script src="dist/bundle.js" type = "text/javascript"></script>
</html>
import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));
{
"compilerOptions": {
"outDir": ".",
"sourceMap": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"module": "commonjs",
"target": "es6",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"traceResolution": true,
"experimentalDecorators": true,
"baseUrl": ".",
}
}
{
"name": "grr",
"version": "0.0.0",
"private": true,
"dependencies": {
"@babel/polyfill": "^7.8.7",
"@panzoom/panzoom": "^4.1.0",
"npm-update-all": "^1.0.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-transform-async-to-generator": "^7.8.3",
"@babel/preset-env": "^7.9.6",
"@babel/preset-react": "^7.9.4",
"@babel/preset-typescript": "^7.9.0",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.1.0",
"typescript": "^3.8.3"
}
}
var webpack = require("webpack");
var path = require('path');
module.exports = (env, options) => {
var PROD = (options.mode === 'production');
return {
entry: [
"@babel/polyfill",
path.resolve(__dirname, "test.ts")
],
output: {
filename: "bundle.js",
libraryTarget: "var"
},
resolve: {
modules: [
'node_modules'
],
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
{
test: /\.tsx?$/,
loaders: [
{
loader: 'babel-loader',
options:
{
compact: false,
presets: [
[
"@babel/preset-env",
{
targets: "> 0.25%, not dead"
}
]
]
}
},
'awesome-typescript-loader'
]
}
]
},
devtool : false,
optimization: {
minimize: PROD
}
}
};
{
"presets": ["@babel/env"],
"plugins": ["@babel/transform-async-to-generator"],
"compact":false
}
最佳答案
我已成功通过将 "esModuleInterop": true
添加到 tsconfig.json
来修复此问题。
https://www.typescriptlang.org/docs/handbook/compiler-options.html
Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility.
这对我来说没有任何意义,但这里有更多信息:
关于javascript - Typescript/babel 导入导致 "_1.default is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61524130/
我是一名优秀的程序员,十分优秀!