Debug & Run Angular2 Typescript with Visual Studio Code?

后端 未结 10 1048
情歌与酒
情歌与酒 2020-11-27 10:08

Debug & Run Angular2 Typescript with Visual Studio Code?

I am trying to debug Angular2 typescript application with VS code https://angular.io/gu

相关标签:
10条回答
  • 2020-11-27 10:45

    This one is tried and tested -

    Step 1: Install chrome debugger: simply open the Command Palette (Ctrl+Shift+P) inside VS Code and type Extensions: Install Extension command. When the extension list appears, type 'chrome' to filter the list and install the Debugger for Chrome extension. You'll then create a launch-configuration file.

    [More Details of Step 1]

    Step 2: Create and update launch.json file: Two example launch.json configs with "request": "launch". You must specify either file or url to launch Chrome against a local file or a url. If you use a url, set webRoot to the directory that files are served from. This can be either an absolute path or a path using ${workspaceRoot} (the folder open in Code). webRoot is used to resolve urls (like "http://localhost/app.js") to a file on disk (like "/Users/me/project/app.js"), so be careful that it's set correctly. update Content of launch.json file as follows-

    {
        "version": "0.1.0",
        "configurations": [
            {
                "name": "Launch localhost",
                "type": "chrome",
                "request": "launch",
                "url": "http://localhost/some_name",
                "webRoot": "${workspaceRoot}/wwwroot"
            },
            {
                "name": "Launch index.html (disable sourcemaps)",
                "type": "chrome",
                "request": "launch",
                "sourceMaps": false,
                "file": "${workspaceRoot}/index.html"
            },
        ]
    }
    

    [More Details of Step 2]

    0 讨论(0)
  • 2020-11-27 10:56

    After lot of research, I found these steps-

    Before you begin, make sure you have latest version of VS code. You can verify latest version – Help > Check For Updates or About.

    1. Install extension called 'Debugger for Chrome'. Once install complete, restart VS code.

    2. Go to Debug window, open launch.json using Chrome.

    3. In Launch.json configuration section, use below config

      {
          "name": "Launch localhost with sourcemaps",
          "type": "chrome",
          "request": "launch",
          "url": "http://localhost:3000/Welcome",
          "sourceMaps": true,
          "webRoot": "${workspaceRoot}"
      }
      
    4. In tsconfig.json, make sure you have "sourceMap": true

    This completes your debug environment settings. Now, before you start debugging, make sure all your existing Chrome.exe instances are closed. Verify from Task Manager OR Use DOS command ‘killall chrome’

    1. Run your project, using npm start command and Chrome as default browser.

    2. Once application is run successfully, you will receive port number. Copy URL from chrome browser and paste into url section above. (NOTE: If you are using routing in your application then url would like above otherwise it will be ending index.html etc)

    3. Now, place breakpoints wherever you want in your typescript files.

    4. Again, go to debug window in VS code, and hit Run. Your tab/instance connected to debugger will looks like below.

    0 讨论(0)
  • 2020-11-27 10:59

    I had issues with this and while @Sankets answer helped it was this issue that solved it for me https://github.com/angular/angular-cli/issues/2453.

    Specifically adding the below to launch.json

    "sourceMapPathOverrides": {
        "webpack:///c:/foo/bar/angular-project/*": "${workspaceRoot}/*"
    }
    
    0 讨论(0)
  • 2020-11-27 11:02

    These mods to launch.json worked for me on MacOS, which enabled me to get the debugger & breakpoints working in a new instance of Chrome per debug session...

    // This forces chrome to run a brand new instance, allowing existing
    // chrome windows to stay open.
    "userDataDir": "${workspaceRoot}/.vscode/chrome",
    "webRoot": "${workspaceRoot}",
    "sourceMaps": true,
    "sourceMapPathOverrides": { "webpack:///./*": "${webRoot}/*" }
    
    0 讨论(0)
提交回复
热议问题