1. 程式人生 > >使用Visual Studio Code開啟瀏覽器檢視HTML檔案

使用Visual Studio Code開啟瀏覽器檢視HTML檔案

vscode出來之前一直使用sublime,後者在編寫HTML檔案時可以通過點選滑鼠右鍵,找到open in browser來啟動系統預設瀏覽器,而vscode卻沒有這個功能,除錯和預覽起來比較麻煩。不過可以通過配置tasks.json檔案來解決這個問題。

一、首先按下Ctrl+shift+p輸入Configure Task ,點選Enter,點選使用模板建立tasks.json檔案,點選MSbuild,然後開啟tasks.json檔案,然後再檔案中修改某些鍵值對。下面是tasks.json檔案的原始內容和部分內容修改如下:

原始內容:

{ // See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "C:/Users/rush/AppData/Local/Google/Chrome/Application/chrome.exe", "args": [ // Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true", "/t:build" ], "group": "build", "presentation": { // Reveal the output only if unrecognized errors occur. "reveal": "silent" }, // Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile" } ] }


修改後內容:

{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Run HTML file with Chrome", "type": "process", // [shell,process] "command": "Chrome", "args": [ "${file}"], "windows": { "command": "C:/Users/rush/AppData/Local/Google/Chrome/Application/chrome.exe", }, "group": "build", "presentation": { // Reveal the output only if unrecognized errors occur. "reveal": "never" //[always,never,silent] }, // Use the standard MS compiler pattern to detect errors, warnings and infos "problemMatcher": "$msCompile" } ] }


二、最後儲存下,按下Ctrl+shift+b,就可以看到瀏覽器自動開啟index.html檔案。