在Visual Studio Code中调试Angular CLI应用程序

在Visual Studio Code中调试Angular CLI应用程序 (Debugging Angular CLI Applications in Visual Studio Code)

In this post, we are going to create an Angular CLI application, then add configuration to debug it in Visual Studio Code.

在本文中,我们将创建一个Angular CLI应用程序,然后添加配置以在Visual Studio Code中对其进行调试。

TLDR-对于Angular CLI应用程序,在VS Code中创建调试配置,安装Debugger for Chrome扩展,然后在调试模式下运行。 (TLDR - For an Angular CLI application, create a debug configuration in VS Code, install the Debugger for Chrome extension, then run in debug mode.)

<!-- TOC -->

<!-目录->

<!-- /TOC -->

<!-/目录->

学习VS代码 (Learn VS Code)

学习VS代码

If you're interested in learning more about VS Code, you definitely want to check out the upcoming Learn VS Code course.

如果您有兴趣了解有关VS Code的更多信息,那么您一定要查看即将推出的“ 学习VS Code”课程。

创建一个入门项目 (Creating a Starter Project)

To be able to test an Angular CLI application, you need an Angular CLI application :) I'll provide the basic steps, but for more reference on how to get started look at the Angular CLI page.

为了能够测试Angular CLI应用程序,您需要一个Angular CLI应用程序:)我将提供基本步骤,但是有关如何入门的更多参考,请参见Angular CLI页面

角度CLI

First, you'll need to install the Angular CLI.

首先,您需要安装Angular CLI。

npm install -g @angular/cli

npm install -g @angular/cli

After that finishes, you'll need to actually generate your new application. This will take a bit as it needs to install LOTS of NPM packages.

完成之后,您将需要实际生成新的应用程序。 这将需要一些时间,因为它需要安装NPM软件包的LOTS。

ng new my-app

ng new my-app

Open the project in VS Code and you should see the following.

在VS Code中打开项目,您应该看到以下内容。

VS Code中的Angular CLI项目

Now, that you've got your new fancy Angular app, go ahead and run it to make sure everything looks right.

现在,您已经有了新的精美Angular应用程序,继续运行它以确保一切正常。

ng serve

ng serve

Should look like this.

应该看起来像这样。

Angular CLI应用程序运行

创建调试配置 (Creating Debug Configuration)

Assuming you've made it this far, we are ready to start debugging! Before we do, however, it's worth understanding how configuring debugging in VS Code works. Basically debug configurations are saved in a launch.json file which is stored inside of a .vscode folder. This .vscode folder is used to store different configurations for Code including our required debugging stuff.

假设您已经做到了这一点,我们就可以开始调试了! 但是,在进行此操作之前,有必要了解VS Code中配置调试的工作方式。 基本上,调试配置保存在启动中 。 存放在中的json文件。 vscode文件夹。 这个 。 vscode文件夹用于存储Code的不同配置,包括我们所需的调试内容。

Before you create your debug configuration, you need to install the Debugger for Chrome extension. Find and install this extension from the extension tab in VS Code. After installing, reload VS Code.

在创建调试配置之前,您需要安装Debugger for Chrome扩展程序。 从VS Code的扩展选项卡中找到并安装此扩展。 安装后,重新加载VS Code。

Chrome调试器

Now, to create a debug configuration, you can open the debug panel (the bug looking button on the left panel). At the top of the debug panel, you should see a dropdown that says "No Configurations".

现在,要创建调试配置,您可以打开调试面板(左侧面板上的bug查找按钮)。 在调试面板的顶部,您应该看到一个下拉菜单,显示“无配置”。

创建调试配置

To the right of that dropdown, there is a gear icon. Click this button to have VS Code automatically generate that .vscode folder and launch.json file mentioned above.

在该下拉菜单的右侧,有一个齿轮图标。 单击此按钮.vscode VS Code自动生成上述的.vscode文件夹和launch.json文件。

Then choose Chrome.

然后选择Chrome。

You should get the following configuration created for you.

您应该为您创建以下配置。

Angular CLI调试配置

The only thing we need to do is update the port from 8080 to 4200.

我们唯一需要做的就是将端口从8080更新到4200。

更新了Angular CLI调试配置

让我们调试 (Let's Debug)

Now we're ready! Go ahead and click the play button at the top of the Debug panel which will launch an instance of Chrome in debug mode. Keep in mind your app should already be running from using ng serve earlier. In VS Code, you should see the Debug toolbar pop up.

现在我们准备好了! 继续并单击“调试”面板顶部的播放按钮,这将在调试模式下启动Chrome实例。 请记住,您的应用程序应该早已通过使用ng serve来运行。 在VS Code中,应该会看到“调试”工具栏弹出。

With this up and running, you can set a breakpoint in your App.component.ts. Open up your App.component.ts and add a breakpoint inside of the component by clicking in the gutter (to the left of the line numbers). Should look like this.

通过运行,您可以在App.component.ts中设置一个断点。 打开您的App.component.ts并通过单击装订线(在行号左侧)在组件内部添加断点。 应该看起来像这样。

Now, refresh debugging by clicking the refresh button on the debugging toolbar. This should open your application again and trigger this breakpoin. You should be directed back to VS Code directly to the place where you set your breakpoint.

现在,通过单击调试工具栏上的刷新按钮来刷新调试。 这将再次打开您的应用程序并触发此断点。 您应该直接回到VS Code,直接回到设置断点的位置。

From here, you can set more breakpoints, inspect variables, etc. If you are interested in learning more about debugging JavaScript in general in either Chrome or VS Code you can check out Debugging JavaScript in Chrome and Visual Studio Code.

从这里,您可以设置更多的断点,检查变量等。如果您想了解有关在Chrome或VS Code中调试JavaScript的更多信息,可以在Chrome和Visual Studio Code中查看Debugging JavaScript

Again, if you're interested in learning more about VS Code, you'll definitely want to check out the upcoming Learn VS Code course.

同样,如果您有兴趣学习有关VS Code的更多信息,那么您肯定会想了解即将举行的Learn VS Code课程。

If you have any follow up questions or comments, leave one below of find me on twitter @jamesqquick.

如果您有任何后续问题或意见,请在twitter @jamesqquick上留下以下“找到我”的内容

For video content, check out my YouTube Channel

有关视频内容,请访问我的YouTube频道

翻译自: https://scotch.io/tutorials/debugging-angular-cli-applications-in-visual-studio-code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值