如何使用VSCode调试Angular?


Answers:


178

经过Angular 5 / CLI 1.5.5测试

  1. 安装Chrome调试器扩展
  2. 创建launch.json(在.vscode文件夹中)
  3. 用我的launch.json(见下文)
  4. 创建tasks.json(在.vscode文件夹中)
  5. 用我的tasks.json(见下文)
  6. CTRL+ SHIFT+B
  7. F5

launch.json for angular/cli >= 1.3

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Chrome",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200/#",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "Attach Chrome",
      "type": "chrome",
      "request": "attach",
      "url": "http://localhost:4200/#",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "Launch Chrome (Test)",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:9876/debug.html",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "Launch Chrome (E2E)",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/protractor/bin/protractor",
      "protocol": "inspector",
      "args": ["${workspaceFolder}/protractor.conf.js"]
    }
  ]
}

tasks.json for angular/cli >= 1.3

{
    "version": "2.0.0",
    "tasks": [
      {
        "identifier": "ng serve",
        "type": "npm",
        "script": "start",
        "problemMatcher": [],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      },
      {
        "identifier": "ng test",
        "type": "npm",
        "script": "test",
        "problemMatcher": [],
        "group": {
          "kind": "test",
          "isDefault": true
        }
      }
    ]
  }

经过Angular 2.4.8测试

  1. 安装Chrome调试器扩展
  2. 创建 launch.json
  3. 用我的launch.json(见下文)
  4. 启动NG Live开发服务器(ng serve
  5. F5

launch.json for angular/cli >= 1.0.0-beta.32

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "http://localhost:4200",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "userDataDir": "${workspaceFolder}/.vscode/chrome",
      "runtimeArgs": [
        "--disable-session-crashed-bubble"
      ]
    },
    {
      "name": "Attach Chrome",
      "type": "chrome",
      "request": "attach",
      "url": "http://localhost:4200",
      "port": 9222,
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true
    }
  ]
}

launch.json for angular/cli < 1.0.0-beta.32

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Lunch Chrome",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200",
      "webRoot": "${workspaceFolder}/src/app",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "webpack:///./~/*": "${workspaceFolder}/node_modules/*",
        "webpack:///./src/*": "${workspaceFolder}/src/*"
      },
      "userDataDir": "${workspaceFolder}/.vscode/chrome",
      "runtimeArgs": [
        "--disable-session-crashed-bubble"
      ]
    },
    {
      "name": "Attach Chrome",
      "type": "chrome",
      "request": "attach",
      "url": "http://localhost:4200",
      "port": 9222,
      "webRoot": "${workspaceFolder}/src/app",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "webpack:///./~/*": "${workspaceFolder}/node_modules/*",
        "webpack:///./src/*": "${workspaceFolder}/src/*"
      }
    }
  ]
}

7
你知道如何下手NG Live Development Server,然后启动Chrome,在短短一个F5点击?
Merdan Gochmuradov '17

2
抱歉,这是不可能的,因为必须在preLaunchTask中启动“ ng serve”任务。“ ng serve”必须永久运行,因此“ preLaunchTask”未完成,这意味着vs代码不会启动调试过程。但我添加了新的配置,应该会使工作更快一些;-)
Asesjix

1
答案简短明了。如果您可以简短地解释一下launch.jsontasks.json在这里做,那就太好了。据我了解,launch.json是启动节点服务器并监听端口8080,并tasks.json指示使用npm和执行命令ng serve来运行该应用程序。
shaijut

@Zachscs您使用了哪个角度版本?
Asesjix

1
我遇到了同样的问题,没有设置断点,直到最后我意识到我的webroot错误。我的默认值为webRoot(“ webRoot”:“ $ {workspaceFolder}”),而不是$ {workspaceFolder} / my-app-folder
Joseph Simpson

48

看起来VS Code团队现在正在存储调试配方。

https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Chrome with ng serve",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200",
      "webRoot": "${workspaceRoot}"
    },
    {
      "name": "Launch Chrome with ng test",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:9876/debug.html",
      "webRoot": "${workspaceRoot}"
    },
    {
      "name": "Launch ng e2e",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/protractor/bin/protractor",
      "protocol": "inspector",
      "args": ["${workspaceRoot}/protractor.conf.js"]
    }      
  ]
}

10

有两种不同的方法。您可以启动新流程或将其附加到现有流程。

这两个过程的关键点是同时运行webpack开发服务器和VSCode调试器

启动一个过程

  1. launch.json文件中添加以下配置:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Angular debugging session",
          "type": "chrome",
          "request": "launch",
          "url": "http://localhost:4200",
          "webRoot": "${workspaceFolder}"
        }
      ]
    }
    
  2. 通过执行以下命令从Angular CLI运行Webpack开发服务器 npm start

  3. 转到VSCode调试器并运行“ Angular调试会话”配置。结果,将打开包含您的应用程序的新浏览器窗口。

附加到现有流程

  1. 为此,您需要在打开端口的调试器模式下运行Chrome(在我的情况下为9222):

    苹果电脑:

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
    

    视窗:

    chrome.exe --remote-debugging-port=9222
    
  2. launch.json 文件将以以下方式显示:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Chrome Attach",
          "type": "chrome",
          "request": "attach",
          "port": 9222,
          "url": "http://localhost:4200/",
          "webRoot": "${workspaceFolder}"
        } 
      ]
    }
    
  3. 通过执行以下命令从Angular CLI运行Webpack开发服务器 npm start
  4. 选择“ Chrome Attach”配置并运行它。

在这种情况下,调试器将附加到现有的Chrome进程中,而不是启动新窗口。

我写了自己的文章,并在其中举例说明了这种方法。

简单说明如何在VSCode中为Angular配置调试器


谢谢,每次启动chrome时,我都必须运行此命令chrome.exe --remote-debugging-port=9222。一次性配置是否还有其他选择
Saurabh47g,

根据您的凭据,您可以右键单击Windows开始菜单中的chrome,点击属性,然后在其中添加标记。这对我的工作计算机不起作用,因此我在git bash中为Windows使用了别名命令。
vitale232


3

可以使用此配置:

{
 "version": "0.2.0",
"configurations": [
{
        "name": "ng serve",
        "type": "chrome",
        "request": "launch",
        "preLaunchTask": "npm: start",
        "url": "http://localhost:8080",
        "webRoot": "${workspaceFolder}"
      }
]
}

2

这是一个更轻便的解决方案,可用于Angular 2+(我正在使用Angular 4)

如果您运行MEAN堆栈,还添加了Express Server的设置。

{
  // Use IntelliSense to learn about possible Node.js debug attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Angular Client",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200",
      "runtimeArgs": [
        "--user-data-dir",
        "--remote-debugging-port=9222"
        ],
        "sourceMaps": true,
        "trace": true,
        "webRoot": "${workspaceRoot}/client/",
        "userDataDir": "${workspaceRoot}/.vscode/chrome"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Express Server",
      "program": "${workspaceRoot}/server/bin/www",
      "outFiles": [
        "${workspaceRoot}/out/**/*.js"
      ]
    }
  ]
}

您可以与此配置同时调试/断点服务器端代码吗?
Mika571

@ Mika571不幸的是,我现在尝试了此操作。我也想同时调试服务器和客户端。
Leniel Maccaferri

1

@Asesjix答案非常彻底,但是正如某些人指出的那样,仍然需要多次交互才能启动ng serve,然后在Chrome中启动Angular应用程序。我使用以下配置单击一下就可以使用此功能。与@Asesjix答案的主要区别是任务类型现在shell是,命令调用start之前已经添加,ng serve因此ng serve可以存在于自己的进程中,并且不会阻止调试器启动:

task.json:

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "ng serve",
        "type": "shell",
        "command": "\"start ng serve\""
      },
      {
        "label": "ng test",
        "type": "shell",
        "command": "\"start ng test\"",
      }
    ]
  }

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch in Chrome",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:4200",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "ng serve"
        }
    ]
}

0

就我而言,我没有使用原始的Angular项目文件夹树,我知道webRoot/ {workspaceFolder}位出了点问题,但是我所有的实验都没有结果。从另一个SO答案中获得了一个提示,如果再次找到它,我将其链接。

帮助我的是{workspaceFolder}在运行时查找变量的内容,然后将其修改为您的“ src”文件夹中的“ app / *”位置。为了找到它,我preLaunchTask在launch.json文件中添加了一个,并输出了值的任务{workspaceFolder}

launch.json,在安装Chrome调试器后显示

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome against localhost",
      "url": "http://localhost:4200",
      "webRoot": "${workspaceFolder}/src/newProjectRoot/",
      "preLaunchTask": "Echo values" //Just to see what the cryptic vs code variables actually are https://code.visualstudio.com/docs/editor/variables-reference
    }
  ]
}

Tasks.json默认情况下不存在。按Ctrl + Shift + p,我认为它称为“为其他命令创建任务”或类似的名称。创建task.json后似乎看不到它。您也可以在与launch.json相同的位置创建文件

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Echo values",
      "command": "echo",
      "args": ["${env:USERNAME}", "workspaceFolder = ${workspaceFolder}"],
      "type": "shell"
    }
  ]
}

知道$ {workspaceFolder}的值后,便将其固定为指向新项目树中的src文件夹,并且一切正常。调试要求ng serve已作为启动前任务或作为构建的一部分(上述示例)或在命令提示符下运行。

是您可以使用的所有变量的链接:

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.