javascript - 未捕获的 SyntaxError:在从 Typescript 编译的代码中
问题描述
我在一个 html 文件中使用了我的 Build Javascript 代码,我收到了这个错误:
未捕获的语法错误:请求的模块“./calculate.js”未提供名为“calculate”的导出
这是打字稿。我不知道我做错了什么。
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"
通过在配置中设置或使用--module
cli 参数将模块代码生成指定为“ES2015” 。
推荐阅读
- ios - 使用结构而不是数组填充 UITableView 数据源
- google-app-engine - 限制我的 Google App Engine 应用程序访问 Google API 密钥
- javascript - 显示JS函数的返回值
元素
- ios - 使用 iTMSTransporter 更新应用内购买?
- java - 对于 AWS Lambda,Build Native 太慢了
- java - 带参数的本机查询 - 在文本值中转义双引号
- java - Java用括号分割字符串
- r - 为数据帧中的每个组分配随机整数
- javascript - 如何使用单击事件侦听器制作按钮切换显示?
- php - 为什么我的 PHP recaptcha 联系表没有将消息发送到指定的电子邮件?