简体   繁体   English

在VSCode中调试Typescript

[英]Debugging Typescript in VSCode

I see a lot of information out there for what seems to be older versions of VSCode (v1.16.1 - latest as of this post) or attributes in the launch.json file that are deprecated. 我看到很多信息似乎是旧版本的VSCode(v1.16.1 - 本文最新版本)或者不推荐使用的launch.json文件中的属性。

I've tried a myriad of config attributes, some older information out there on GitHub forums (some are not viable since the attributes are gone, or deprecated). 我已经尝试过无数的配置属性,GitHub论坛上有一些较旧的信息(有些不可行,因为属性已经消失或被弃用)。 Trying to debug and hit breakpoints directly in the Typescript code within VSCode. 尝试直接在VSCode中的Typescript代码中调试和命中断点。

Currently, here is my tsconfig.json file: 目前,这是我的tsconfig.json文件:

    {
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/",
    "baseUrl": "src",
    "module": "commonjs",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "include": [
      "src/**/*.ts"
  ],
    "exclude": [
      "node_modules"
  ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

My launch.json file for Visual Studio Code is: 我的Visual Studio Code的launch.json文件是:

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "Launch Chrome",
          "type": "chrome",
          "request": "launch",
          "url": "http://localhost:3000/",
          "sourceMaps": true,
          "trace": true,
          "webRoot": "${workspaceRoot}/src",
          "userDataDir": "${workspaceRoot}/.vscode/chrome",
          "sourceMapPathOverrides": {
              "webpack:///src/*": "${webRoot}/*"
          }
      }
  ]
}

Chrome opens up, but the standard "...refused connection" page is displayed: Chrome已打开,但会显示标准的“...拒绝连接”页面: 拒绝连接......

Right now, just trying to debug one of those typical "admin templates". 现在,只是尝试调试其中一个典型的“管理模板”。 I can run it perfectly fine through the terminal: ng serve 我可以通过终端完美地运行它:ng服务

However, debugging this Angular app is eluding me. 但是,调试这个Angular应用程序是在逃避我。 Worth noting that I'm completely new to Angular, Typescript and VSCode in general. 值得注意的是,我对Angular,Typescript和VSCode一般都是新手。

Have you tried to specify the outFiles attribute of your configuration to something like: 您是否尝试将配置的outFiles属性指定为:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome",
            "sourceMaps": true,
            "outFiles": [
                "${workspaceRoot}/out/**/*.js"
            ]
        }
    ]
}

According to this docs the setting "sourceMaps": true tells vscode to map the generated *.js with the *.ts files. 根据这个文档 ,设置"sourceMaps": true告诉vscode将生成的*.js映射到*.ts文件。 And furthermore you need to set the outFiles attribute to tell vscode where it should look for those *.js files if they are not in the same direcotry. 此外,您需要设置outFiles属性以告诉vscode,如果它们不在同一个目录中,它应该查找那些*.js文件。

From the docs: 来自文档:

If the generated (transpiled) JavaScript files do not live next to their source but in a separate directory, you must help the VS Code debugger locating them by setting the outFiles attribute. 如果生成的(已转换的)JavaScript文件不在其源旁边但位于单独的目录中,则必须通过设置outFiles属性来帮助VS Code调试器找到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM