How to debug an Angular 2 application with Chrome and VS Code

In this blogpost I want to show you how to debug an Angular 2 application with Chrome and VS Code.

First of all you need to install the extension in VS Code.

You can find it here

https://github.com/Microsoft/vscode-chrome-debug

or search in the extensions tab for the plugin directly:

How to debug an Angular 2 application with Chrome and VS CodeAfter installing you probably have to enable the plugin and restart VS Code but in the end you will see your folder structure like normal. Then head over to the debug tab and press the button for creating you an new configuration and select the “Chrome” environment.

howtodebuganangular2applicationwithchromeandvscode_02After doing this the extension created a new folder (if you do not have it already) called “.vscode” and a “launch.json” in it initially looking like this:

 1 {
 2     "version": "0.2.0",
 3     "configurations": [
 4         {
 5             "name": "Launch Chrome against localhost, with sourcemaps",
 6             "type": "chrome",
 7             "request": "launch",
 8             "url": "http://localhost:8080",
 9             "sourceMaps": true,
10             "webRoot": "${workspaceRoot}"
11         },
12         {
13             "name": "Attach to Chrome, with sourcemaps",
14             "type": "chrome",
15             "request": "attach",
16             "port": 9222,
17             "sourceMaps": true,
18             "webRoot": "${workspaceRoot}"
19         }
20     ]
21 }

Our folder strucutre tells us that the files are served from the root.

howtodebuganangular2applicationwithchromeandvscode_03So the “”webRoot”: “${workspaceRoot}”” -setting is good to go for us. We will open a new Chrome instance but it needs an existing running server. So if you use something like “lite-server” you can easily type “lite-server” at the root of your webapplication or place it in your npm command chain in the “npm start” command. This is what I did. But before we go we need to sdjust the urls where the server is running on and the url where the Chrome instance is starting.

So replace the port in the config file with the port from your lite-server. In my cae thats “3000”. This is how your config look like then:

 1 {
 2     "version": "0.2.0",
 3     "configurations": [
 4         {
 5             "name": "Launch Chrome against localhost, with sourcemaps",
 6             "type": "chrome",
 7             "request": "launch",
 8             "url": "http://localhost:3000",
 9             "sourceMaps": true,
10             "webRoot": "${workspaceRoot}"
11         },
12         {
13             "name": "Attach to Chrome, with sourcemaps",
14             "type": "chrome",
15             "request": "attach",
16             "port": 9222,
17             "sourceMaps": true,
18             "webRoot": "${workspaceRoot}"
19         }
20     ]
21 }

Then start the lite server and just hit “play”

howtodebuganangular2applicationwithchromeandvscode_04

howtodebuganangular2applicationwithchromeandvscode

Chrome starts and you can debug your page in VS Code. Of course you can also confugre Chrome to attach directly. See here for examples:

https://github.com/Microsoft/vscode-chrome-debug/wiki/Examples

Hope this helps anybody

BR

Fabian

原文地址:https://www.cnblogs.com/cjxhd/p/6129985.html