欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何通過(guò)vscode運(yùn)行調(diào)試javascript代碼

 更新時(shí)間:2020年07月24日 09:09:48   作者:ttweixiao9999  
這篇文章主要介紹了如何通過(guò)vscode運(yùn)行調(diào)試javascript代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

初次正式要寫(xiě) javascript 相關(guān)的代碼,想要用 vscode 直接編譯 js 代碼,但是發(fā)現(xiàn)沒(méi)有那么簡(jiǎn)單,需要配置好 launch.json 文件,現(xiàn)已經(jīng)在vscode上編譯過(guò)去并且可以調(diào)試 javascript 代碼,總結(jié)了兩種方法,分享給大家.

方法一: 在 js 后綴文件中寫(xiě) javascript 代碼.

1. 環(huán)境配置:

(1). 需要安裝 nodejs (在Bing搜索中輸入 nodejs, 找到nodejs官網(wǎng),然后找到適合你電腦配置的安裝包進(jìn)行下載安裝,最后要輸入 node -v 和 npm -v 檢驗(yàn)是否安裝成功)

(2). 可以安裝 vscode 擴(kuò)展包: Code Runner

2. 新建一個(gè) js 后綴的文件,如 hello_world.js ,輸入以下內(nèi)容:

var a = 1;
var b = 2;
console.log("hello world");
console.log("a = ", a);
console.log("b = ", b);

3. 運(yùn)行程序

(1) 如果你安裝了 Code Runer,那么就可以直接點(diǎn)擊右鍵選擇 Run Code 運(yùn)行代碼,就可以在 OUTPUT 窗口上看到運(yùn)行結(jié)果

(2) 在 vscode 的 TERMINAL 終端輸入: node hello_world.js 也可以看到 運(yùn)行結(jié)果

(3) 想要按下 F5 進(jìn)行運(yùn)行并且調(diào)試,那么就要配置好 launch.json 文件. 先點(diǎn)擊 Run -> Open Configurations, 輸入以下內(nèi)容

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",

  "configurations": [{
    "name": "Launch",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/hello_world.js",
    },
  ]
}

注意這里的第 11 行的文件名稱(chēng)要改成你自己定義的文件名稱(chēng),其中的workspaceRoot 表示當(dāng)前文件路徑.

再按下 F5 的時(shí)候,記得配置文件一定要選擇名為 Launch (和上面的name同名) 的那個(gè)配置文件運(yùn)行,配置了 launch.json 文件,你還可以在 js 文件中打上斷點(diǎn)進(jìn)行調(diào)試.如下圖所示

方法二: 在 html 后綴文件中寫(xiě) javascript 代碼.

1. 環(huán)境配置:

(1) 安裝 chrome 瀏覽器(做前端開(kāi)發(fā)這是通用瀏覽器)

(2) 安裝 vscode 擴(kuò)展包: Debugger for chrome 和 open in browser

(3) File -> Preferences -> Settings, 輸入 breakpoints,找到 Debug: Allow Breakpoints Everywhere,勾上允許在任何文件設(shè)置斷點(diǎn)(這樣才可以在html文件中設(shè)置斷點(diǎn))

2. 新建一個(gè) html 后綴的文件,如 a.html ,輸入以下內(nèi)容:

<!DOCTYPE html>
<html>

<head>
<script>
function myFunction()
{
  console.log("hello world");
  document.getElementById("demo").innerHTML="My First JavaScript Function";
  alert("this is a place where can write code.");
}
</script>
</head>

<body>

<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>

3. 運(yùn)行程序

(1) 按下 F5 運(yùn)行并且調(diào)試代碼,這里主要涉及到 launch.json 文件的配置,先點(diǎn)擊 Run -> Open Configurations, 輸入以下內(nèi)容

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome against localhost",
      "url": "http://localhost:8080",
      "webRoot": "${workspaceFolder}"
    },
    {
      "type": "chrome",
      "request": "launch",
      "name": "使用本地chrome調(diào)試",
      "file": "${file}",
      "port":8000,
    }
  ]
}

然后在 script 的代碼部分打上斷點(diǎn),按下 F5 , 點(diǎn)擊 Try it 按鈕,你可以看到中間結(jié)果了,如下圖所示

(2) 鼠標(biāo)右鍵點(diǎn)擊 Open in Other Browsers, 選擇其中 一個(gè)瀏覽器就可以看到結(jié)果,再點(diǎn)擊按鈕出現(xiàn)的網(wǎng)頁(yè)中的 Try it 按鍵,就可以調(diào)用 script 中 js 的代碼的結(jié)果. 這里,你也可以在vscode中設(shè)置你的默認(rèn)瀏覽器,那么你就可以選擇Open in Default Browers, 在默認(rèn)的瀏覽器中打開(kāi), 或者按下快捷鍵 Alt + B 查看結(jié)果. (這種方法不能調(diào)試,并且這種方法只能在配置好launch.json后再按下F5之后才可以使用)

(備注: vscode 默認(rèn)瀏覽器的設(shè)置, File -> Preferences -> Settings, 輸入 default browser , 找到 Open-in-browser : Default , 我這里是輸入了 : Google Chrome )

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論