这是一个更轻松的解决方案,适用于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" ] } ] }
launch.json
tasks.json
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 } } ] }
ng serve
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/*" } } ] }
这有两种不同的方式。您可以 发射 一个新的过程或 连接 到现有的。
这两个过程的关键点是 webpack dev服务器和VSCode调试器同时运行 。
在你的 launch.json 文件添加以下配置:
{ "version": "0.2.0", "configurations": [ { "name": "Angular debugging session", "type": "chrome", "request": "launch", "url": "http://localhost:4200", "webRoot": "${workspaceFolder}" } ] }
通过执行从Angular CLI运行Webpack dev服务器 npm start
npm start
为此,您需要在调试器模式下使用已打开的端口运行Chrome(在我的情况下,它将是 9222 ):
9222
苹果电脑:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
视窗:
chrome.exe --remote-debugging-port=9222
launch.json 文件将以下列方式查找:
{ "version": "0.2.0", "configurations": [ { "name": "Chrome Attach", "type": "chrome", "request": "attach", "port": 9222, "url": "http://localhost:4200/", "webRoot": "${workspaceFolder}" } ] }
在这种情况下,调试程序附加到现有的Chrome进程,而不是启动新窗口。
我写了自己的文章,在那里我用插图描述了这种方法。
简单说明如何在VSCode中为Angular配置调试器
这在Visual Studio代码站点上有详细解释: https://code.visualstudio.com/docs/nodejs/angular-tutorial