日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用vs2022在.net6中調試帶typescript的靜態頁面_基礎應用

作者:萬金流 ? 更新時間: 2022-03-13 編程語言

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"http://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"http://網站根目錄
});
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>

運行結果:

原文鏈接:https://www.cnblogs.com/wanjinliu/p/15547055.html

欄目分類
最近更新