如何配置Angular和VSCode,使我的断点正常工作?
如何配置Angular和VSCode,使我的断点正常工作?
launch.json
(位于 .vscode 文件夹内)launch.json
(见下文)tasks.json
(位于 .vscode 文件夹内)tasks.json
(见下文)angular/cli >= 1.3 的 launch.json
{
"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"]
}
]
}
angular/cli >= 1.3
的 tasks.json
{
"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
}
}
]
}
launch.json
文件launch.json
(见下文)ng serve
)angular/cli >= 1.0.0-beta.32的launch.json配置
{
"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
}
]
}
angular/cli < 1.0.0-beta.32 的 launch.json
{
"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/*"
}
}
]
}
F5
键就能启动 NG Live Development Server
并打开 Chrome
吗? - Merdan Gochmuradovlaunch.json
和 tasks.json
在这里的作用就更好了。据我所知,launch.json
用于启动节点服务器并监听端口8080,而 tasks.json
则指示使用 npm
并执行命令 ng serve
来运行应用程序。 - Shaiju T看起来 Visual Studio 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"]
}
]
}
有两种不同的方法来做到这一点。您可以启动一个新进程或附加到一个现有进程。
这两个过程的关键是同时运行 webpack dev server 和 VSCode 调试器。
在您的 launch.json
文件中添加以下配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Angular debugging session",
"type": "chrome",
"request": "launch",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}"
}
]
}
通过执行npm start
命令从Angular CLI中运行Webpack开发服务器。
为此,您需要以调试器模式运行Chrome,并打开端口(在我的情况下是9222
):
Mac:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Windows:
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}"
}
]
}
npm start
命令在 Angular CLI 中运行 Webpack dev server。在这种情况下,调试器会附加到现有的 Chrome 进程,而不是启动一个新窗口。
我写了一篇文章,其中描述了这种方法,并配以插图。
chrome.exe --remote-debugging-port=9222
。有没有一种替代方法来进行一次性配置呢? - Saurabh Gangamwar"webRoot": "${workspaceFolder}/client"
。 - M H"webRoot": "${workspaceFolder}/client",
- undefined这在Visual Studio Code网站上有详细的解释:https://code.visualstudio.com/docs/nodejs/angular-tutorial
可以使用这个配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "ng serve",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
ng serve
,然后在Chrome中启动Angular应用程序。我使用以下配置实现了单击即可工作。与@Asesjix的答案的主要区别在于任务类型现在为shell
,命令调用添加了start
在ng serve
之前,因此ng serve
可以存在于自己的进程中,而不会阻止调试器启动:
tasks.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"
}
]
}
start
是一个 Windows CMD 命令。ng serve
是一个 Angular 命令。如果你在使用 Windows CMD 时收到 start my serve
不是有效命令的消息,我猜测你的路径中没有正确安装 Angular。 - ubiquibaconng.ps1
的文件。我能够在一个带有Angular项目的文件夹中运行ng serve
,但是如果我使用start ng serve
,会弹出一个类似的文件ng.ps1
。 - Stephen Phamlaunch.json文件中的compound选项允许您创建一个可以同时启动多个启动配置的单个启动配置。如果您想要例如启动您的Web服务器并附加调试器(为什么不同时启动后端),或者如果您有任何其他特定需求,这将非常有用。
您可以像这样使用它:
{
"version": "0.2.0",
"configurations": [
{
"name": "Angular Debug",
"type": "chrome",
"request": "launch",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"webpack:///./*": "${webRoot}/*"
},
},
{
"name": "Angular Serve",
"command": "ng serve --open",
"request": "launch",
"type": "node-terminal",
"cwd": "${workspaceFolder}",
},
],
"compounds": [
{
"name": "Angular Launch",
"configurations": ["Angular Serve", "Angular Debug"]
}
]
}
这里有一个更轻量级的解决方案,适用于Angular 2+(我使用的是Angular 4)
如果您运行的是MEAN堆栈,还添加了Express服务器的设置。
{
// 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"
]
}
]
}
webRoot
/{workspaceFolder}
这一部分出了问题,但是所有的尝试都没有结果。从另一个SO答案中得到了一个提示,如果我再找到它的话,我会提供链接。
对我有帮助的是,在运行时找到变量{workspaceFolder}
的内容,然后将其修改为我的“src”文件夹的位置,在该文件夹下有“app/*”。为了找到它,我在我的launch.json文件中添加了一个preLaunchTask
和一个任务来输出{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
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo values",
"command": "echo",
"args": ["${env:USERNAME}", "workspaceFolder = ${workspaceFolder}"],
"type": "shell"
}
]
}
一旦我了解了${workspaceFolder}的价值,我就将其修复为指向我的新项目树中的src文件夹,然后一切都正常工作了。调试需要在预启动任务或构建(如上例)中作为一部分运行ng serve
或在命令提示符中运行。
这里是所有可以使用的变量链接:
对于那些已经阅读了以上内容但仍无法正常工作的人,请通过ng version
(在您的项目文件夹中)检查您的Angular和Node.js版本。
它应该输出版本:
ng serve
,请参见此处的答案:链接 - minus one