简体   繁体   English

使用F5在Angular项目中进行vscode TypeScript调试。 没有破裂或在错误的位置

[英]vscode TypeScript debugging in Angular project with F5. Not breaking or in a wrong spot

I read many related questions where debugging does not work. 我读到了许多相关的问题,其中调试不起作用。 In my case it works but it's "weird". 就我而言,它有效,但是“很奇怪”。 I set breakpoint at one line of code but it breaks in completely diferrent line. 我在一行代码中设置了断点,但它在完全不同的行中断了。 I feel it's related to source maps, but not sure what it is. 我觉得它与源地图有关,但不确定是什么。

Environment: 环境:

  • Latest vscode(1.21.1) 最新的vscode(1.21.1)
  • Latest Chrome debugger extension installed 安装了最新的Chrome调试器扩展
  • Lastest npm/angular CLI 最新的npm /角度CLI
  • Latest Chrome (65.0.33...) 最新版Chrome(65.0.33 ...)

Generated project using CLI with ng new 使用CLI和ng new生成的项目

Following config added for F5 launcher(launch.json): 为F5启动器(launch.json)添加了以下配置:

 "configurations": [        
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:4200",
            "webRoot": "${workspaceFolder}"
        }
    ]

This is code of app.component.ts : 这是app.component.ts代码:

import { Component, OnInit } from '@angular/core';
import { isUndefined } from 'util';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = "Portal";

  ngOnInit(): void {

    this.test();
  }

  test() {
    const person = { FirstName: "John" };
    person.FirstName = "sf";

    let b = "test string";

    b = "other string";
  }
}

Set breakpoint on something like b = "other string" 在类似b = "other string"设置断点

Start as usual in terminal ng serve Click F5 - Chrome opens and displays main screen. 在终端ng serve照常开始单击F5-Chrome打开并显示主屏幕。

Problem 1: Chrome doesn't brake at all. 问题1: Chrome根本无法刹车。

When I refresh Chrome page - Breakpoint get hit but in a wrong place. 刷新Chrome页面时-断点被击中,但位置错误。 It can be in ngOnInit or on Component line, pretty "random". 它可以在ngOnInit或在Component行中,相当“随机”。

Problem 2: Breaking happens on a wrong line 问题2:中断发生在错误的行上

Is there any way to make this work so I can set and debug without pain using VSCode? 有什么办法可以使这项工作,以便我可以使用VSCode轻松进行设置和调试吗?

It's hard to say why things behave oddly with your setup. 很难说出为什么您的设置行为异常。 I also use VSCode to debug Angular apps and my setup works pretty well, I'll share it here so that hopefully it can be of help. 我还使用VSCode调试Angular应用程序,并且我的设置运行良好,我将在这里共享它,希望对您有所帮助。

 { "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "Launch Chrome", "url": "http://localhost:4200", "webRoot": "${workspaceRoot}", "sourceMaps": true, "userDataDir": "${workspaceRoot}/.vscode/chrome", "runtimeArgs": [ "--disable-session-crashed-bubble" ] }, { "name": "Attach Chrome", "type": "chrome", "request": "attach", "url": "http://localhost:4200", "port": 9222, "webRoot": "${workspaceRoot}", "sourceMaps": true } ] } 

  • Serve your angular app with ng serve ng serve您的角度应用ng serve

You should now be able to start debugging your app (with F5 ) and all the breakpoints should work fine. 现在,您应该能够开始调试应用程序(使用F5 ),并且所有断点都可以正常工作。

This seems to be related to this question: 这似乎与以下问题有关:

Angular CLI 1.7.0 and Visual Studio Code - can't set breakpoints Angular CLI 1.7.0和Visual Studio Code-无法设置断点

The solution there was to install an older version of the angular cli. 解决方案是安装较旧版本的cli。

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

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