本文我们将使用angular-cli搭建我们的第一个angular项目。1、安装Node.js和npm包管理工具下载Node.js:推荐下载8.12.0版本,比较稳定,下载后安装即可。2.安装vscode下载vscode安装vscode时勾选以下选项插件推荐VisualStudioCode中文(简体)语言包:VSCodeAngular7Snippets中文(简体)语言包-TypeScript,Html,AngularMaterial,ngRx,RxJS&FlexLayout:Angular代码片段VSCodesimpleIconswithAngular:Angular文件图标样式TSLint:TypeScript代码规范工具Beautiful:代码格式化插件3.安装相关环境打开vscode后,使用快捷键Ctrl+`orView->终端,打开终端。安装angular-cli语句:npminstall-g@angular/cli科学上网:使用淘宝镜像解决部分安装包无法下载的问题。(可能会导致包安装问题)npminstall-gcnpm--registry=https://registry.npm.taobao.org4.构建项目在你的电脑上创建一个angular文件夹来存放你的项目,然后创建一个文件文件夹,右键单击该文件夹并选择使用vscode打开。终端路径会自动打开到当前文件夹。使用命令创建一个新项目。ngnewmy-app由于网络问题,新建项目时最好加上--skipInstall跳过安装步骤,然后使用npminstall安装。ngnewmy-app--skipInstallcdmy-appnpminstall注意:从外观上看,angular项目是无法使用cnpm安装的,所以尽量不要安装cnpm。但是如果不使用cnpm,可能会因为网络问题没有安装好node-sass。解决办法:使用淘宝镜像下载npmconfigsetsass_binary_sitehttps://npm.taobao.org/mirrors/node-sass/onlyfornode-sass5.在终端输入以下命令运行项目,项目地址:http://localhost:4200/ngserve--open这样我们的项目就可以运行了。接下来,修改/src/app/app.component.ts如下:import{Component}from'@angular/core';@Component({selector:'app-root',templateUrl:'./app.component.html',styleUrls:['./app.component.less']})exportclassAppComponent{title='my-app';hello='HelloWorld!';}在/src/app/app.component.ts文件中在
