VsCode工具debug代码调试
1.概述
这篇文章介绍VsCode工具通过插件调试代码。
当我们写完一个功能运行的时候,界面中没有显示出我们想要的内容。这个时候通过盲目的翻看代码找出问题的位置比较耗时间,效率低下。我们通过debug插件可以快速的定位到问题的原因,提升定位问题的效率。
2.安装debug插件
2.1.搜索debugger for Chrome插件安装
2.2.创建两个文件测试debug
在项目的任意位置创建debug文件夹,在文件夹下新建index.html和index.js文件。
- 我这在src目录下新建debug文件夹,和index.html、index.js
- index.html文件内容
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>debug调试</title>
<script type="text/javascript" src="./index.js"></script>
</head>
<body>
<h1>debug调试</h1>
</body>
</html>
- index.js文件内容
const array = ['A', 'B', 'C', 'D', 'E']
for (let index = 0; index < array.length; index++) {
const element = array[index]
console.log(element)
}
2.3.配置debug插件launch.json
安装的debug for Chrome插件可以通过配置文件修改他的内容改变他的用法。
- 点击debug图标选择chrome
- 配置launch.json文件内容如下
{
// 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 index.html",
"sourceMaps": true,
// 新建index.html文件的位置
"file": "${workspaceRoot}/src/debug/index.html"
}
]
}
2.4.运行debug查看调试日志
在debug界面切换到Launch index.json,点击前面的运行按钮
- 查看控制台打印出调试日志
3.debug调试应用到项目
前面我们通过一个debug文件测试了调试功能,下面我们将debug应用到项目的调试中。
3.1.配置launch.json文件内容如下
{
// 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": "vuejs: chrome",
"url": "http://localhost:8868",
"webRoot": "${workspaceFolder}/src",
"breakOnLoad": true,
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
}
]
}
3.2.启动Vue项目
这里使用的是Vue3.x版本脚手架项目,启动命令和Vue2不同。如果是Vue2的使用Vue2启动项目命令即可
# 进入项目根路径
cd vue-manage-system
# 启动项目
npm run serve
3.3.调试vue项目
在Debug模式下点击启动vuejs:chrome按钮,使用debug模式启动项目。启动后在DEBUG CONSOLE控制台可以看到输出日志。