首页 > 解决方案 > 在 Typescript 中导出和导入模块

问题描述

我正在尝试导出一个简单的类,如下所示:
functions.ts

 export class components{
        private m_name:string;
        private m_level:string;
    
        constructor(name:string,level:string){
            this.m_name=name;
            this.m_level=level;
        }
    
    }

应用程序.ts

import {components} from './functions';
let c1 = new components("Tushar","Admin");

我包括被转译成 index.html 的 js 文件(app.js),如果它正在工作,可以在控制台上查看它。 索引.html

<!DOCTYPE html>
<html>
    <head>
        <title>
            Feedback
        </title>
    </head>
    <body>
        <script src= "app.js"></script>
    </body>
</html>

这是tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "commonjs",
     "strict": true,
     "esModuleInterop": true,
     "skipLibCheck": true,
     "forceConsistentCasingInFileNames": true
 }
}    

我收到一条错误消息:

未捕获的 ReferenceError:未在 app.js:2 中定义导出

我不明白为什么它不起作用。我尝试将目标更改为 es6 但没有帮助。虽然这在repl.it上运行良好

标签: javascriptnode.jstypescript

解决方案


推荐阅读