前置
在 HTML 中使用 type="module"
时:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module</title>
<script type="module" src="index.js"></script>
</head>
<body>Hello Module</body>
</html>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
其中,index.js
由 tsc
生成,其中:
// index.ts
import { method1 } from "./modules/m1"
1
2
2
编译后得到的结果为:
// index.js
import { method1 } from "./modules/m1"
1
2
2
由于缺乏后缀名称,那么浏览器访问时自然就 404 了。
解决方案
直接增加 .ts
后缀?那么很明显会得到 导入路径不能以“.ts”扩展名结束。
的错误。
根据 TypeScript 编译器 tsc 命令能够自动补全完整的 js 后缀? (opens new window) 在导入时增加 .js
后缀,最后生效了。
TypeScript 社区也对此进行了长期的讨论,并给出了一份总结 (opens new window)。