这个问题通常由源代码映射错误引起。在生产代码中,Angular编译器会将所有组件和模块压缩到一起,因此在调试应用程序时,Angular调试器将代码位置指向main.js文件,这使得调试变得困难。但是,可以通过正确配置source-map来解决这个问题。
你可以在angular.json文件中添加以下代码:
"sourceMap": { "scripts": true, "styles": true, "vendor": true }
或者,在tsconfig.json文件中添加以下配置:
{ "compilerOptions": { "sourceMap": true, "inlineSources": true, "inlineSourceMap": true, ... } }
这样,当你在调试器中设置断点时,它将在正确的位置停止,而不是在压缩后的main.js文件中。
例如,以下代码段会在home.component.ts文件中设置断点,而不是在main.js中:
@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit { constructor() { }
ngOnInit() { debugger; } }
这样,在调试应用程序时,调试器将在home.component.ts中设置断点。