未捕获的语法错误:在从 Typescript 编译的代码中



我在 html 文件中使用了我的 Build Javascript 代码,但出现此错误:

未捕获的语法错误:请求的模块"./计算.js"没有 提供名为"计算"的导出

这是打字稿。我不知道我做错了什么。

class calculate{
public firstNumber:string;
public secondNumber:string;
public resultBox:string;
constructor() { }

greet(){
console.log("Hello my");
}

}
export {calculate};

下面是 JavaScript 代码:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class calculate {
constructor() { }
greet() {
console.log("Hello my");
}
}
exports.calculate = calculate;

这是我的 HTML 文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
</body>
<script  type="module">
import {calculate} from './calculate.js'
var cal= new calculate();
cal.greet();
</script>
</html>

您正在编译为 commonjs 模块格式。通过在配置中设置"module":"ES2015"或使用 cli 参数--module将模块代码生成指定为"ES2015"。

演示

最新更新