在vs2022和.net6中調試帶typescript的靜態頁面


如題

1、新建一個空的web項目

 

 2、設計、建好目錄結構

 

 其中ts存放typescript源文件,web為網站根目錄,scripts/js存放ts生成的js腳本。

index.html為靜態網頁。

3、新建ts配置文件tsconfig.json,修改內容為:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "outDir": "web/scripts/js"//ts編譯出js的輸出目錄
  },
  "include": ["ts/**/*"],//ts所在位置。“**/”為任意層級目錄,“?”和“*”為一般通配符。
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

4、修改program.cs,指定web文件夾,並支持靜態內容。

//var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    WebRootPath = "web"//網站根目錄
});
var app = builder.Build();
app.UseDefaultFiles();//支持默認文件(index.html)
app.UseStaticFiles();//啟用靜態文件支持
//app.MapGet("/", () => "Hello World!");

app.Run();

5、寫一個簡單的ts文件f1.ts

document.getElementById('s1').innerHTML="I'm comming...."

其實這里是簡單的js內容而已

6、編譯之后,會生成js

  7、index.html內容如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    aaa<span id="s1"></span>
    <script src="/scripts/js/f1.js"></script>
</body>
</html>

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM