答案:通过VS Code的复合启动配置可同时调试前后端。首先在launch.json中定义独立的前后端调试配置,然后添加compounds字段组合它们,最后在运行面板选择复合配置启动,实现一键调试全栈应用。

在 .vscode/launch.json 文件中,确保你已经为前端和后端分别定义了可工作的调试配置。例如:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Backend (Node.js)",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/server.js",
      "console": "integratedTerminal"
    },
    {
      "name": "Launch Frontend (React/Vue)",
      "type": "pwa-chrome",
      "request": "launch",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/frontend/src",
      "browserFocus": true
    }
  ]
}
在 launch.json 的根对象中添加 compounds 字段,将上述两个配置组合起来:
{
  "version": "0.2.0",
  "configurations": [ ... ],
  "compounds": [
    {
      "name": "Debug Full Stack",
      "configurations": ["Launch Backend (Node.js)", "Launch Frontend (React/Vue)"]
    }
  ]
}
注意:configurations 数组中的名称必须与已有配置的 name 完全一致。
打开 VS Code 的运行和调试侧边栏,在顶部下拉菜单中选择 Debug Full Stack,然后点击启动按钮。
立即学习“前端免费学习笔记(深入)”;
若希望确保后端先启动完成再打开前端页面,可在后端配置中使用 console: "integratedTerminal" 并手动确认服务就绪。更高级的做法是结合 preLaunchTask 使用脚本检测端口占用或添加延迟。
例如,使用 npm script 启动两个服务并由 compound 调用:
"scripts": {
  "dev:debug": "concurrently \"npm run start:backend\" \"npm run start:frontend\""
}
然后在 launch.json 中调用该脚本:
{
  "name": "Launch via Script",
  "type": "node",
  "request": "launch",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run", "dev:debug"],
  "console": "integratedTerminal"
}
以上就是如何创建复合启动配置(Compound Launch Configs)来同时调试前端和后端?的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                 
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号