Visual Studio 使用 AngularJS 进行代码调试

Visual Studio Code debugging with AngularJS

我想用新的 Visual Studio 代码调试我的 Angular 应用程序,但是 Angular 和 Visual Studio 代码似乎有问题..

这是我的 launch.json:

{
    "version": "0.1.0",
    // List of configurations. Add new configurations or edit existing ones.  
    // ONLY "node" and "mono" are supported, change "type" to switch.
    "configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Manager",
            // Type of configuration. Possible values: "node", "mono".
            "type": "node",
            // Workspace relative or absolute path to the program.
            "program": "/Volumes/Transcend/WorkArea/Manager/app/app.js",
            // Automatically stop program after launch.
            "stopOnEntry": true,
            // Command line arguments passed to the program.
            "args": [],
            // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
            "cwd": ".",
            // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
            "runtimeExecutable": null,
            // Optional arguments passed to the runtime executable.
            "runtimeArguments": [],
            // Environment variables passed to the program.
            "env": { },
            // Use JavaScript source maps (if they exist).
            "sourceMaps": false
        }, 
        {
            "name": "Attach",
            "type": "node",
            // TCP/IP address. Default is "localhost".
            "address": "localhost",
            // Port to attach to.
            "port": 5858,
            "sourceMaps": false
        }
    ]
}

当我尝试调试我的 Angular 应用程序时出现此错误,

错误:

ReferenceError: angular is not defined
    at Object.<anonymous> (/Volumes/Transcend/WorkArea/Manager/app/app.js:1:79)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain [as _onTimeout] (module.js:497:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
MacBook-Pro:Manager user$ cd '/Volumes/Transcend/WorkArea/Manager';  'node' '--debug-brk=55539' '/Volumes/Transcend/WorkArea/Manager/app/app.js'
debugger listening on port 55539

Killed: 9

app.js

/// <reference path="../typings/angularjs/angular.d.ts"/>

var routerApp = angular.module('uiRouter', ['ui.router', 'uiRouter.dmvs']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/home');

    $stateProvider

        .state('home', {
            url: '/home',
            templateUrl: 'app/dmvs/partial-d.html',
            controller:'dController'
        })

});

好的,在@code 人员的帮助下,我让它工作了。我现在可以从 IDE 完全调试 Angular 客户端!希望这会对其他人有所帮助...

首先,您需要下载 "Debugger for Chrome Extension." 您可以输入以下内容:

F1
ext Install Extensions
debug (then select Debugger For Chrome)

安装后,我使用了此处找到的 MSFT 说明:

https://marketplace.visualstudio.com/items/msjsdiag.debugger-for-chrome

我只能使用 "attach" 方法,所以我将其与 Chrome 一起使用。这是我使用的 launch.son 文件的最终版本:

{
    "version": "0.2.0",
    "configurations": [
        {
            // Use this to get debug version of Chrome running:
            // /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
            "name": "Attach",
            "type": "chrome",
            "request": "attach",
            "port": 9222,
            "webRoot": "./www"
        }
    ]
}

此外,不要忘记在调试模式下启动 Chrome(对于 Mac):

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

伟大的编辑@code!

OBS:不要忘记杀死所有 Chrome 个实例,如此处所述:https://github.com/Microsoft/vscode-chrome-debug/issues/111#issuecomment-189508090

我遇到了类似的问题,但我的项目还包含导致上述解决方案失败的 webpack。遍历互联网后,我在 github:

上的一个线程中找到了解决方案

https://github.com/AngularClass/angular2-webpack-starter/issues/144#issuecomment-218721972

总之,就是这样了。

注意:- 在开始之前,您必须检查您是否拥有最新版本的 visual studio 代码并且还安装了名为 'Debugger for Chrome 的扩展' 在 VS 代码中。

首先,在'./config/webpack.dev.js'

  • 使用 => 开发工具:'source-map'
  • 而不是 => devtool:'cheap-module-source-map'

然后安装并使用write-file-webpack-plugin:

  • npm install --save write-file-webpack-plugin

通过添加以下内容将插件添加到“./config/webpack.dev.js”:

  • const WriteFilePlugin = require('write-file-webpack-plugin');

在顶部的 Webpack 插件下。继续补充:

  • new WriteFilePlugin()

到new new DefinePlugin()后的插件列表,即

plugins:[
    new DefinePlugin({....}),
    new WriteFilePlugin(),
    ....
]

这确保将源映射写入磁盘

最后,下面给出我的launch.json

{
    "version": "0.2.0",
    "configurations": [{
        "name": "Launch Chrome against localhost, with sourcemaps",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:3000/",
        "runtimeArgs": [
           "--user-data-dir",
           "--remote-debugging-port=9222"
        ],
        "sourceMaps": true,
        "diagnosticLogging": true,
        "webRoot": "${workspaceRoot}",
        "userDataDir": "${workspaceRoot}/.vscode/chrome"
    },
    {
        "name": "Attach to Chrome, with sourcemaps",
        "type": "chrome",
        "request": "attach",
        "url": "http://localhost:3000/",
        "port": 9222,
        "sourceMaps": true,
        "diagnosticLogging": true,
        "webRoot": "${workspaceRoot}"
    }]
}

注意在 webroot 中没有'/dist/'。使用此配置,source-maps 在 ./dist/ 中,但它们指向 ./src/。 vscode 将绝对根添加到工作区,并正确解析文件。

我们正在使用 Gulp.js 并且必须添加以下内容:

"sourceMapPathOverrides": {
    "/source/*":"${workspaceRoot}/[directory where all of our mappings are located]/*"
 }

希望这对尝试使用 VS Code 调试 angularjs 应用程序的人有所帮助。

这是一个示例配置:

{
"version": "0.2.0",
"configurations": [
    {
        "type": "chrome",
        "request": "launch",
        "name": "Launch Chrome",
        "url": "[your url here]",
        "webRoot": "${workspaceRoot}/[directory where your app is located]",
        "sourceMaps": true,        
        "sourceMapPathOverrides": {
            "/source/*":"${workspaceRoot}/[directory where your app is located and any additional .js files that are required by your app]/*"
        },        
        "userDataDir": "${workspaceRoot}/.vscode/chrome",
        "runtimeArgs": [
            "--disable-session-crashed-bubble"
        ]
    }
]
}