首页 > 解决方案 > 未捕获的 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>

标签: javascripttypescript

解决方案


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

演示


推荐阅读