在Vs code中使用debugger for chrome

如何配置debugger for chrome,包含attach、launch两种模式的介绍,讲述关键步骤应该呈现的状态。实际安装过程中出现的一些问题该如何解决。

欢迎补足。

下载插件

3004133-49ead2d3b8f284f6.PNG
插件安装.PNG

安装完后记得重新加载

3004133-e6457a8446581001.PNG
重新加载页面.PNG

将自己的项目先运行起来,我是用vue-cli搭建的。
注意要开启source-map功能
https://cn.vuejs.org/v2/cookbook/debugging-in-vscode.html#在浏览器中展示源代码

切换到debugger模式,为自己的项目添加配置

3004133-bf39901668fcc707.PNG
切换到debugger模式.PNG

选择chrome 环境后,你会得到下面这个文件


3004133-7d2145671b42a5ac.PNG
配置文件.PNG

request 是指具体的模式 有launch和attach两种

launch 模式会新打开一个浏览器窗口

url 浏览器标签页的url,这里要输入项目的有效地址
webRoot 源代码的位置,这一项非常重要,如果出现打断点不起作用或者断电的位置不对都与此项配置有关。

下面是我的配置,仅供参考!

{
// 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",
        "url": "http://192.168.10.144:8080/#/",
        "webRoot": "${workspaceFolder}/src",
        "sourceMaps": true
      }
  ]
}

"webRoot": "${workspaceFolder}/src" 指向的位置就是项目中src文件夹


3004133-2aff82f6c77acf91.PNG
src文件夹.PNG

在源代码中设置一个断点,正常境况下断点是红色的


3004133-663619f79cf37648.PNG
断点.PNG

启动插件

3004133-31596664922104b0.PNG
启动插件.PNG

点击绿色按钮启动后,vs code 的显示是这样的,你可以Ctrl + ` 打开控制台查看输出


3004133-7fc8b2f48d6808e7.PNG
vscode开启debugger后.PNG

同时会新弹出一个浏览器窗口


3004133-a524843eefdf01f9.PNG
新弹出浏览器窗口.PNG

这时候,触发断点,正常境况下浏览器显示为


3004133-1bdc914d6ed80ba8.PNG
触发debugger后浏览器的样子.PNG

vscode 显示为


3004133-29ed1d8176289ac3.PNG
触发debugger后vscode的显示.PNG

如果控制台打印错误
(Error processing "setBreakpoints": Error: Could not resolve breakpoint): 需要再次检验项目是否开启sourcemap。

attach的开启

该模式是基于已开启浏览器窗口的监听,在配置上

url 一定是当前浏览器显示的路径,否则启动时会报错

3004133-fc5655e4cea5e4b9.PNG
attach模式url配置错误.PNG

下面是我的配置,仅供参考

{
// 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,
        "url": "http://192.168.10.144:8080/#/",
        "webRoot": "${workspaceFolder}/src",
        "sourceMaps": true
    }
]
}

要开启chrome浏览器的远程调试端口

找到chrome浏览器快捷方式所在的文件夹 在目标一栏末尾添加一个空格后 在加入 --remote-debugging-port=9222

3004133-466bcb2934c615c4.PNG
开启远程端口.PNG

注意端口要和配置的一致

点击确定后 重启浏览器,请直接双击配置好的快捷方式,其他地方的快捷方式可能为何其产生关联从而导致程序失效,报如下错误


3004133-6383f727abd7aee9.PNG
attach无法连接错误.PNG

配置好后 开启成功后的样式如下。

3004133-10f89113c89a7a9f.PNG
正常开启attach模式.PNG

参考链接

https://blog.csdn.net/crisschan/article/details/79970813

https://www.jianshu.com/p/a176dcf1301c

推荐一下我自己github项目

https://github.com/xiaodun/sf-pc-web/tree/v1.0.0/tools_home
https://github.com/xiaodun/sf-mobile-web/tree/master/fell_pain

猜你喜欢

转载自blog.csdn.net/weixin_34161029/article/details/86948343
今日推荐