本文转自:https://code.visualstudio.com/docs/nodejs/angular-tutorial

Using Angular in Visual Studio Code

Angular is a popular JavaScript library for building web application user interfaces developed by Google. The Visual Studio Code editor supports Angular IntelliSense and code navigation out of the box.

Welcome to Angular

We'll be using the Angular CLI for this tutorial. To install and use the command line interface as well as run the Angular application server, you'll need the Node.js JavaScript runtime and npm (the Node.js package manager) installed. npm is included with Node.js which you can install from here.

Tip: To test that you have Node.js and npm correctly installed on your machine, you can type node --version and npm --version.

To install the Angular CLI, in a terminal or command prompt type:

npm install -g @angular/cli

This may take a few minutes to install. You can now create a new Angular application by typing:

ng new my-app

my-app is the name of the folder for your application. This may take a few minutes to create the Angular application in TypeScript and install its dependencies.

Let's quickly run our Angular application by navigating to the new folder and typing ng serve to start the web server and open the application in a browser:

cd my-app
ng serve

You should see "Welcome to app!!" on http://localhost:4200 in your browser. We'll leave the web server running while we look at the application with VS Code.

To open your Angular application in VS Code, open another terminal (or command prompt) and navigate to the my-app folder and type code .:

cd my-app
code .

Syntax highlighting and bracket matching

Now expand the src\app folder and select the app.component.ts file. You'll notice that VS Code has syntax highlighting for the various source code elements and, if you put the cursor on a parentheses, the matching bracket is also selected.

IntelliSense

As you hover your mouse over text in the file, you'll see that VS Code gives you information about key items in your source code. Items such as variables, classes and Angular decorators are a few examples where you'll be presented with this information.

As you start typing in app.component.ts, you'll see smart suggestions and code snippets.

You can click the information button (i) to see a flyout with more documentation.

VS Code uses the TypeScript language service for code intelligence (IntelliSense) and it has a feature called Automatic Type Acquisition (ATA). ATA pulls down the npm Type Declaration files (*.d.ts) for the npm modules referenced in the package.json.

Go to Definition, Peek definition

Through the TypeScript language service, VS Code can also provide type definition information in the editor through Go to Definition (F12) or Peek Definition (Ctrl+Shift+F12). Open the app.module.tsfile and put the cursor over AppComponent in the bootstrap property declaration, right click and select Peek Definition. A Peek window will open showing the AppComponent definition from app.component.ts.

Press Escape to close the Peek window.

Hello World!

Let's update the sample application to "Hello World". Go back to the app.component.ts file and change the title string in AppComponent to "Hello World".

import { Component } from '@angular/core';@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Hello World'; } 

Once you save the app.component.ts file, the running instance of the server will update the web page and you'll see "Welcome to Hello World!!".

Tip: VS Code supports Auto Save, which by default saves your files after a delay. Check the Auto Save option in the File menu to turn on Auto Save or directly configure the files.autoSave user setting.

Debugging Angular

To debug the client side Angular code, we'll need to install the Debugger for Chrome extension.

Note: This tutorial assumes you have the Chrome browser installed. The builders of the Debugger for Chrome extension also have versions for the Safari on iOS and Edge browsers.

Open the Extensions view (Ctrl+Shift+X) and type 'chrome` in the search box. You'll see several extensions which reference Chrome.

Press the Install button for Debugger for Chrome. The button will change to Installing then, after completing the installation, it will change to Reload. Press Reload to restart VS Code and activate the extension.

Set a breakpoint

To set a breakpoint in app.component.ts, click on the gutter to the left of the line numbers. This will set a breakpoint which will be visible as a red circle.

Configure the Chrome debugger

We need to initially configure the debugger. To do so, go to the Debug view (Ctrl+Shift+D) and click on gear button to create a launch.json debugger configuration file. Choose Chrome from the Select Environment dropdown. This will create a launch.json file in a new .vscode folder in your project which includes a configuration to launch the website.

We need to make one change for our example: change the port of the url from 8080 to 4200. Your launch.json should look like this:

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

Press F5 or the green arrow to launch the debugger and open a new browser instance. The source code where the breakpoint is set runs on startup before the debugger was attached so we won't hit the breakpoint until we refresh the web page. Refresh the page and you should hit your breakpoint.

You can step through your source code (F10), inspect variables such as AppComponent, and see the call stack of the client side Angular application.

The Debugger for Chrome extension README has lots of information on other configurations, working with sourcemaps, and troubleshooting. You can review it directly within VS Code from the Extensions view by clicking on the extension item and opening the Details view.

Popular Starter Kits

In this tutorial, we used the Angular CLI to create a simple Angular application. There are lots of great samples and starter kits available to help build your first Angular application.

Recipes

The VS Code team has created recipes for more complex debugging scenarios. There you'll find the Chrome Debugging with Angular CLI recipe which also uses the Angular CLI and goes into detail on debugging the generated project's unit tests.

MEAN Starter

If you'd like to see a full MEAN (MongoDB, Express, Angular, Node.js) stack example, look at MEAN.IO. They have documentation and an application generator for a sample MEAN project. You'll need to install and start MongoDB, but you'll quickly have a MEAN application running. VS Code also has great MongoDB support through the Azure Cosmos DB extension.

React

React is another popular web framework. If you'd like to see an example of React working with VS Code, check out the Using React in VS Code tutorial. It will walk you through creating an React application and configuring the launch.json file for the Debugger for Chrome extension.

Angular Extensions

In addition to the functionality VS Code provides out of the box, you can install VS Code extensions for greater functionality.

Angular v6 Snippets
3.4M

johnpapa

Angular v6 snippets by John Papa

Angular 6 Snippets -...
3.3M

Mikael

219 Angular Snippets (TypeScript, Html, Angular M...

Angular Language Ser...
768.3K

Angular

Editor services for Angular templates

Angular Essentials
166.1K

johnpapa

Essential extensions for Angular developers

Click on an extension tile above to read the description and reviews on the Marketplace.

To find other Angular extensions, open the Extensions view (Ctrl+Shift+X) and type 'angular' to see a filtered list of Angular extensions.

The community has also created "Extension Packs" which bundle useful extensions together (for example, a linter, debugger, and snippets) into a single download. To see available Angular extension packs, add the "extension packs" category to your filter (angular @category:"extension packs").

[转]Using Angular in Visual Studio Code相关推荐

  1. 如何使用 Visual Studio Code 调试 Angular Schematics 实现

    How to Debug an Angular Schematic using Visual Studio Code 能够在代码执行时调试和遍历代码是我们开发工作流程的基础.这是一项必不可少的功能,使 ...

  2. 如何处理Angular项目在Visual Studio Code打开报关于@Decorators的警告信息

    我最近用Visual Studio Code开发Angular项目时,打开app.component.ts, 就遇到如下的警告消息: Experimental support for decorato ...

  3. [北京微软技术直通车]前端 Visual Studio Code 开发 Angular 与亿级 SQL Servern

    微软技术直通车第一期将于2017年9月9日与大家见面,本次邀请华北区微软技术专家和大家一起交流前端工具与技术, Visual Studio Code,TypeScript 与 Anuglar 项目开发 ...

  4. Angular14 Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题、emmet安装...

    前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...

  5. Visual Studio Code搭建TypeScript开发环境

    转载 http://www.cnblogs.com/sunjie9606/p/5945540.html 使用Visual Studio Code搭建TypeScript开发环境 1.TypeScrip ...

  6. Visual Studio Code的用户设置相关

    快捷键ctrl ,打开设置,或者在命令提示行里,输入open settings: workspace setting: located under the .vscode folder in your ...

  7. 打造TypeScript的Visual Studio Code开发环境

    打造TypeScript的Visual Studio Code开发环境 本文转自:https://zhuanlan.zhihu.com/p/21611724 作者: 2gua TypeScript是由 ...

  8. Visual Studio Code预览版Ver 0.3.0试用体验

    当你开始阅读这篇文章时,请先不要把Visual Studio Code和.net.Windows联想到一起,因为VS Code是一个跨平台,支持30多种语言的开箱代码编辑器.不管你是.Net.Java ...

  9. vscode 注释多行代码_如何在Visual Studio Code中注释多行?

    我找不到在Visual Studio Code中注释和取消注释多行代码的方法. 是否可以使用某些快捷方式在Visual Studio Code中注释和取消注释多行? 如果是,该怎么办? 当其中一行已被 ...

最新文章

  1. linux useradd
  2. Spring中@Pattern的使用
  3. 叛逆的仔:不听老板的话,还要用行动给老板来个响亮耳光!
  4. 【FI】-【AM】跨工厂/事业部/利润中心的固定资产调拨
  5. C语言实现面向对象示例
  6. lisp java_ai lisp java
  7. css margin和border,Margin、Border、Padding属性的区别和联系
  8. 机器学习(三十二)——t-SNE, Adaboost
  9. Oracle用户密码过期问题解决
  10. 微服务Springcloud超详细教程+实战(二)
  11. 华为手机安卓系统可能停更,鸿蒙系统手机要来了
  12. ssh整合步骤之一(搭建环境)
  13. php接口和多态的概念以及简单应用
  14. python aiml开发文档,带有Python的AIML文件
  15. html页面乱码解决
  16. 热烈庆祝阳光网驿-行业软件交流平台与北京汉邦极通科技有限公司成功合作
  17. 线性表的链式存储结构 ( 链表 )
  18. iic上拉电阻的阻值计算方法与特性
  19. java中线程池的实现原理:七参、四策
  20. 69 MyBatis和Spring整合

热门文章

  1. 博途上载hmi程序_西门子HMI触摸屏(精智面板)恢复出厂设置方法
  2. 判断手机号码是否符合要求
  3. [BUUCTF-pwn]——picoctf_2018_buffer overflow 1
  4. 关于android的外文论文,关于android的外文文献.doc
  5. java 内存屏障类型_Java内存模型精讲
  6. 15.5 匿名内部类
  7. jdbc增删改查有哪些步骤_用Mybatis如何实现对数据库的增删改查步骤
  8. Struts2 在页面定义变量 s:set标签
  9. 3-12形参和实参 局部变量
  10. Linux下gcc编译生成动态链接库*.so文件并调用它