vs code之Live Server的使用(搭建本地服務(wù)器)

最近接手了個(gè)jquery的項(xiàng)目,由于每次修改都要清緩存重新加載才能看到效果,所以研究了一下Live Server,在本地搭建了一個(gè)服務(wù)器,實(shí)現(xiàn)了實(shí)時(shí)預(yù)覽,十分方便。

第一步:下載并安裝Live Server

image.png

可以看到,vs code右下角出現(xiàn)了這樣一個(gè)小圖標(biāo)(點(diǎn)擊即可打開)



如不作任何配置:點(diǎn)擊之后默認(rèn)打開http://127.0.0.1:5500


如果是單純的html文件想要實(shí)現(xiàn)實(shí)時(shí)預(yù)覽,配置到這就夠了,但由于我這里是一個(gè)項(xiàng)目,有網(wǎng)絡(luò)請求,所以如果主機(jī)名為127.0.0.1會(huì)出現(xiàn)跨域的問題(后端設(shè)置允許訪問的端口為:localhost:9000端口),下面的步驟就是完成這個(gè)工作的。

第二步:詳細(xì)配置

點(diǎn)擊擴(kuò)展設(shè)置,打開settings.json



具體添加的配置如下(最重要的是前兩條配置):

 "liveServer.settings.port": 9000, //設(shè)置本地服務(wù)的端口號(hào)
 "liveServer.settings.host": "localhost",//主機(jī)名配置,默認(rèn)127.0.0.1
 "liveServer.settings.root": "/", //設(shè)置根目錄,也就是打開的文件會(huì)在該目錄下找
 "liveServer.settings.CustomBrowser": "chrome", //設(shè)置默認(rèn)打開的瀏覽器
 "liveServer.settings.NoBrowser": false,
  "liveServer.settings.ignoredFiles": [ //設(shè)置忽略的文件
    ".vscode/**",
    "**/*.scss",
    "**/*.sass"
  ],
 "liveServer.settings.ChromeDebuggingAttachment": false,//啟用Chrome調(diào)試到Live Server的附件  "liveServer.settings.fullReload": false,
 "liveServer.settings.AdvanceCustomBrowserCmdLine": "", //默認(rèn)情況下,Live Server會(huì)在不完全重新加載瀏覽器的情況下注入CSS更改。您可以通過將此設(shè)置設(shè)置為來更改此行為true,默認(rèn)false

整個(gè)settings.json配置如下:

{
  "workbench.iconTheme": "vscode-icons",
  "editor.codeActionsOnSave": null,
  "files.associations": {
    "*.vue": "vue",
    "*.wpy": "vue",
    "*.wxml": "html",
    "*.wxss": "css"
  },
  "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
  "git.enableSmartCommit": true,
  "git.autofetch": true,
  "emmet.triggerExpansionOnTab": true,
  "emmet.showAbbreviationSuggestions": true,
  "emmet.showExpandedAbbreviation": "always",
  "emmet.includeLanguages": {
    "vue-html": "html",
    "vue": "html",
    "wpy": "html",
    "javascript": "html"
  },
  //主題顏色 
  //"workbench.colorTheme": "Monokai",
  "git.confirmSync": false,
  "explorer.confirmDelete": false,
  "editor.fontSize": 14,
  "editor.wordWrap": "on",
  "editor.detectIndentation": false,
  // 重新設(shè)定tabsize
  "editor.tabSize": 2,
  //失去焦點(diǎn)后自動(dòng)保存
  "files.autoSave": "onFocusChange",
  // #值設(shè)置為true時(shí),每次保存的時(shí)候自動(dòng)格式化;
  "editor.formatOnSave": false,
  //每120行就顯示一條線
  "editor.rulers": [],
  // 在使用搜索功能時(shí),將這些文件夾/文件排除在外
  "search.exclude": {
    "**/node_modules": true,
    "**/bower_components": true,
    "**/target": true,
    "**/logs": true,
  },
  // 這些文件將不會(huì)顯示在工作空間中
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/*.js": {
      "when": "$(basename).ts" //ts編譯后生成的js文件將不會(huì)顯示在工作空中
    },
    "**/node_modules": false
  },
  // #讓vue中的js按"prettier"格式進(jìn)行格式化
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  "vetur.format.defaultFormatter.js": "prettier",
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      // #vue組件中html代碼格式化樣式
      "wrap_attributes": "force-aligned", //也可以設(shè)置為"auto",效果會(huì)不一樣
      "wrap_line_length": 200,
      "end_with_newline": false,
      "semi": false,
      "singleQuote": true
    },
    "prettier": {
      "semi": false,
      "singleQuote": true
    }
  },
  "liveServer.settings.donotShowInfoMsg": true,
  "workbench.colorTheme": "Dracula",
  "diffEditor.ignoreTrimWhitespace": false,
  "liveServer.settings.port": 9000, //設(shè)置本地服務(wù)的端口號(hào)
  "liveServer.settings.host": "localhost",//主機(jī)名配置,默認(rèn)127.0.0.1
  "liveServer.settings.root": "/", //設(shè)置根目錄,也就是打開的文件會(huì)在該目錄下找
  "liveServer.settings.CustomBrowser": "chrome", //設(shè)置默認(rèn)打開的瀏覽器
  "liveServer.settings.NoBrowser": false,
  "liveServer.settings.ignoredFiles": [ //設(shè)置忽略的文件
    ".vscode/**",
    "**/*.scss",
    "**/*.sass"
  ],
  "liveServer.settings.ChromeDebuggingAttachment": false,//啟用Chrome調(diào)試到Live Server的附件
  "liveServer.settings.fullReload": false,
  "liveServer.settings.AdvanceCustomBrowserCmdLine": "", //默認(rèn)情況下,Live Server會(huì)在不完全重新加載瀏覽器的情況下注入CSS更改。您可以通過將此設(shè)置設(shè)置為來更改此行為true,默認(rèn)false
}

更多詳細(xì)配置見:https://github.com/ritwickdey/vscode-live-server/blob/master/docs/settings.md

另:如果想用Debugger for Chrome結(jié)合Live Server使用的話需要先用Live Server打開一個(gè)端口再打開Debugger for Chrome(注意:這兩插件端口必須配置一致,我這里都是localhost:9000),Debugger for Chrome的具體使用見我的這篇文章:vs code之Debugger for Chrome的使用(開啟谷歌調(diào)試)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容