设置 Chrome 远程调试端口
首先我们需要在远程调试打开的状态下启动 Chrome, 这样 VS Code 才能 attach 到 Chrome 上:
Windows
- 右键点击 Chrome 的快捷方式图标,选择属性
- 在目标一栏,最后加上--remote-debugging-port=9222 注意要用空格隔开
macOS
打开控制台执行:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Linux
打开控制台执行:
google-chrome --remote-debugging-port=9222
Visual Stuido Code 安装插件
点击 Visual Studio Code 左侧边栏的扩展按钮, 然后在搜索框输入Debugger for Chrome并安装插件,再输入,安装完成后点击 reload 重启 VS Code
添加 Visual Studio Code 配置
- 点击 Visual Studio Code 左侧边栏的 调试 按钮, 在弹出的调试配置窗口中点击 设置 小齿轮, 然后选择 chrome, VS Code 将会在工作区根目录生成.vscode 目录,里面会有一个 lanch.json 文件并会自动打开
- 用下面的配置文件覆盖自动生成的 lanch.json 文件内容。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { //... "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "vuejs: chrome", "url": "http:192.168.1.210:8010", "webRoot": "${workspaceFolder}/src", "breakOnLoad": false, "sourceMapPathOverrides": { "webpack:///src/*": "${webRoot}/*" } } ] } |
修改 webpack 的 sourcemap
如果你是基于 webpack 打包的 vue 项目, 可能会存在断点不匹配的问题, 还需要做些修改:
- 打开根目录下的 config 目录下的 index.js 文件
- 将dev 节点下的 devtool 值改为 'eval-source-map'
- 将dev节点下的 cacheBusting 值改为 false
开始调试吧
一切具备了, 现在验收成果了
- 通过第一步的方式以远程调试打开的方式打开 Chrome
- 在 vue 项目中执行npm run dev以调试方式启动项目
- 点击 VS Code 左侧边栏的调试按钮,选择 Attach to Chrome 并点击绿色开始按钮,正常情况下就会出现调试控制条。
- 现在就可以在.vue文件的js代码中打断点进行调试了。
// {
// "version": "0.2.0",
// "configurations": [
// {
// "type": "chrome",
// "request": "launch",
// "name": "vuejs: chrome",
// "url": "http://localhost:8080",
// "webRoot": "${workspaceFolder}/src",
// "breakOnLoad": true,
// "sourceMapPathOverrides": {
// "webpack:///src/*": "${webRoot}/*"
// }
// },
// {
// "type": "firefox",
// "request": "launch",
// "name": "vuejs: firefox",
// "url": "http://localhost:8080",
// "webRoot": "${workspaceFolder}/src",
// "pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }],
// "firefoxExecutable": ""
// }
// ]
// }
//附加chrome配置 {
// // 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": "attach",
// "name": "Attach to Chrome",
// "port": 9222,
// "webRoot": "${workspaceRoot}/src",
// "url": "http://localhost:8080/#/",
// "sourceMaps": true,
// "sourceMapPathOverrides": {
// "webpack:///src/*": "${webRoot}/*"
// }
// }
// ]
// }
{
//...
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "vuejs: chrome",
"url": "http://localhost:8080/#/",
"webRoot": "${workspaceFolder}/src",
"breakOnLoad": false,
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
}
本文详细介绍如何设置Chrome浏览器的远程调试端口,以便于使用Visual Studio Code进行高效代码调试。包括在不同操作系统上启动Chrome的步骤,安装VSCode插件,配置lanch.json,以及解决基于webpack的Vue项目断点不匹配问题。
2917

被折叠的 条评论
为什么被折叠?



