如何在打字稿中导入类

时间:2019-01-02 11:34:48

标签: typescript class module export loading

我是打字稿编程的新手。我试图使用打字稿创建一个在浏览器中运行的非常简单的项目。

运行应用程序时出现以下错误;

Uncaught ReferenceError: exports is not defined

我了解问题是正在加载课程。但是我不确定如何解决此问题。

任何人都可以提供一些帮助吗?

我看到了几个需要使用模块加载器执行此操作的站点。但是找不到任何示例来说明如何做到这一点。

我在应用程序中有以下文件

person.ts

    export class Person{
    greet()
    {
        console.log("hello");
    }
}

main.ts

import {Person} from './person'
let p = new Person();
p.greet();

index.html

<!DOCTYPE html>
<html>
    <head>
        <script src="./main.js"></script>
    </head>
    <body>
    </body>
</html>

tsconfig.json

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

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "lite-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "lite-server": "^2.4.0"
  }
}

0 个答案:

没有答案