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

來源:互聯網
上載者:User

標籤:created   cto   ima   creat   meta   play   git   hit   des   

by Fabian Gosebrink on October 16, 2016 in Angular ? 0 Comments

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:

After 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.

After 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.

So 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”

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

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.