【发布时间】:2017-03-19 12:00:15
【问题描述】:
我希望能够使用 Visual Studio Code 调试 Angular2 应用程序。
这是我的环境:
- 操作系统:Ubuntu 16.10 x64
- 浏览器:Chromium 53.0.2785.143
- 节点:6.8.0
- Angular-cli:1.0.0-beta.19-3
使用 angular-cli 创建一个新项目:
ng new test-VSC-debug
cd test-VSC-debug
然后我打开 VSC 并加载项目:File/open folder
我点击debug 徽标并选择chrome configure launch.json。它会生成以下文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Chrome against localhost, with sourcemaps",
"type": "chrome",
"request": "launch",
"url": "http://localhost:8080",
"sourceMaps": true,
"webRoot": "${workspaceRoot}"
},
{
"name": "Attach to Chrome, with sourcemaps",
"type": "chrome",
"request": "attach",
"port": 9222,
"sourceMaps": true,
"webRoot": "${workspaceRoot}"
}
]
}
然后我通过运行来启动 angular2 项目:
ng serve
一旦启动,在 VSC 中我选择:“Launch Chrome against localhost, with sourcemaps”。
然后,我收到以下错误:
“找不到 chrome :安装它或在启动配置中设置 runtimeExecutable 字段。”
所以我设置了:
“runtimeExecutable”:“铬浏览器”
(就像我在我的 Ubuntu 上没有有铬但有铬)。
Angular-cli 启动应用程序的默认端口是 4200。 将网址从“http://localhost:8080”更改为“http://localhost:4200”。
现在浏览器正在打开应用程序,但 VSC 出现以下错误: “无法连接到运行时进程,10000 毫秒后超时 - (原因:无法连接到目标:连接 ECONREFUSED 127.0.0.1:9222”。
从在 stackoverflow/github 问题上找到的其他答案中,我了解到我可能必须在尝试这样做之前杀死所有 chrome 实例,所以我只需关闭 chromium 并运行 killall chromium-browser。
我尝试再次运行调试:与以前相同的错误(无法连接)。
我还看到以下论点可能会有所帮助:
"runtimeArgs": [
"--remote-debugging-port=9222",
"--user-data-dir"
]
但它不会改变任何东西。
我决定将 VSC 用于我的 typescript 开发人员(主要是 angular2),这种调试方式似乎非常强大。我觉得不使用它太糟糕了:)。
感谢您的帮助!
PS:一些相关的stackoverflow问题和github问题:
- Debug & Run Angular2 Typescript with Visual Studio Code?
- https://github.com/angular/angular-cli/issues/2453
- https://github.com/angular/angular-cli/issues/1936
- https://github.com/angular/angular-cli/issues/1281
编辑 1:!!!部分改进!!! 我找到了一种在 Visual Studio Code 控制台中获取调试信息的方法! 所以它还不完美,因为断点不起作用,但这就是问题所在。 到目前为止,如果我打开http://localhost:9222,我什么也看不到。 (“本地主机未授权连接”)。
但是,如果我这样启动铬:
chromium-browser --remote-debugging-port=9222 --user-data-dir=remote-profile
重要的是要注意这个论点:--user-data-dir=remote-profile。如果您只是通过 --user-data-dir 它会启动一个没有连接的新窗口。但这还不够。您需要将 remote-profile 作为值传递。
- 它会打开一个新的浏览器窗口
- 我打开http://localhost:4200 也可以联系到http://localhost:9222!
- 我可以使用“使用源映射附加到 chrome”选项来连接 VSC! (如您所见,我确实有“Angular 2 正在开发模式下运行。调用 enableProdMode() 以启用生产模式。”显示在控制台中,页脚现在有橙色背景)
到目前为止,我希望它可以帮助一些人。 但现在的问题是断点不起作用。
我会继续挖掘,如果找到原因,我会再做一次修改。
【问题讨论】:
-
使用 Angular 2.4.8 stackoverflow.com/questions/42495655/…
标签: angular google-chrome-devtools visual-studio-code angular-cli