初學(xué)TypeScript,嘗試在html引入ts編譯出來的js文件:
報(bào)錯(cuò):Uncaught ReferenceError: exports is not defined
以下是代碼:
創(chuàng)建了TS:加入export {}形成獨(dú)立的作用域,其他ts文件重復(fù)聲明相同名稱的變量。
export {}
let str = "tt";
// str=22 //類型不對,編輯器IDE會提示錯(cuò)誤
let a:string="44"
console.log(str)
tsconfig.json配置:
/* Modules */ "module": "commonjs",
編譯之后生成js:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let str = "tt";
// str=22 //類型不對,編輯器IDE會提示錯(cuò)誤
let a = "44";
console.log(str);
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="01-基礎(chǔ).js"></script>
</head>
<body>
</body>
</html>
運(yùn)行時(shí)報(bào)錯(cuò):Uncaught ReferenceError: exports is not defined。
解決步驟:
? 說是要配置生成ESXX,我把生成模塊類型改成ES2020版本了。
tsconfig.json配置:
/* Modules */ "module": "ES2020",
再次運(yùn)行報(bào)錯(cuò):
Uncaught SyntaxError: export declarations may only appear at top level of a module
引入類型設(shè)置為module
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="module" src="01-基礎(chǔ).js"></script>
</head>
<body>
</body>
</html>
再次運(yùn)行OK:文章來源:http://www.zghlxwxcb.cn/news/detail-417246.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-417246.html
到了這里,關(guān)于HTML引入Typescript編譯JS文件 :Uncaught ReferenceError: exports is not defined的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!