1、背景介绍

关于Angular版本,Angular官方已经统一命名Angular 1.x同一为Angular JS;Angular 2.x及以上统称Angular;

CLI是Command Line Interface的简写,是一种命令行接口,实现自动化开发流程,比如:ionic cli、vue cli等;它可以创建项目、添加文件以及执行一大堆开发任务,比如测试、打包和发布。

官方文档:https://angular.io

官方文档:https://angular.io/guide/quickstart

GitHub:https://github.com/angular/angular-cli

Angular Material:https://material.angular.io/

2、安装Angular CLI

  1. 首先确认安装了node.js和npm

    // 显示当前node和npm版本
    $ node -v
    $ npm -v
    // node 版本高于6.9.3 npm版本高于3.0.0
  2. 全局安装typescript(可选)
    $ npm install -g typescript
    // 新建项目的时候会自动安装typescript(非全局)所以这里也可以不用安装。
  3. 安装Angular CLI
    $ npm install -g @angular/cli

    经过不算漫长的等待,你的Angular CLI就装好了。确认一下:

    $ ng v
    
    // 出现下面画面说明安装成功,如果不成功你可能需要uninstall一下,再重新来过
    $ ng v
    _ _ ____ _ ___
    / \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
    / △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
    / ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
    /_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
    |___/
    @angular/cli: 1.1.1
    node: 7.10.0
    os: darwin x64

3、新建Angular项目

$ ng new my-app

这里要等很久啊,大概要下载141M东西。
如果你已经建好了项目文件夹就可以使用ng init my-app来新建项目,ng init和ng new的区别是ng new会帮我们创建一个和项目名称相同的文件夹。

趁着它在下载,来看一下运行ng new之后Angular cli已经帮我们干了什么:

$ ng new helloKeriy
installing ng
create .editorconfig
create README.md
create src/app/app.component.css // 使用HTML模板、CSS样式和单元测试定义AppComponent组件。 它是根组件,随着应用的成长它会成为一棵组件树的根节点。
create src/app/app.component.html
create src/app/app.component.spec.ts
create src/app/app.component.ts // 定义AppModule,这个根模块会告诉Angular如何组装该应用
create src/app/app.module.ts
create src/assets/.gitkeep // 这个文件夹下你可以放图片等任何东西,在构建应用时,它们全都会拷贝到发布包中。
create src/environments/environment.prod.ts
create src/environments/environment.ts
create src/favicon.ico // 每个网站都希望自己在书签栏中能好看一点。 请把它换成你自己的图标。
create src/index.html // 宿主页面
create src/main.ts
create src/polyfills.ts
create src/styles.css // 公共样式
create src/test.ts // 这是单元测试的主要入口点
create src/tsconfig.app.json
create src/tsconfig.spec.json
create src/typings.d.ts
create .angular-cli.json // Anguar 编译依赖
create e2e/app.e2e-spec.ts // e2e 端对端测试目录
create e2e/app.po.ts
create e2e/tsconfig.e2e.json
create .gitignore
create karma.conf.js
create package.json // Angular 的依赖包
create protractor.conf.js
create tsconfig.json // TypeScript 编译器的参数
create tslint.json
Successfully initialized git.
Installing packages for tooling via npm.
Installed packages for tooling via npm.
Project 'helloKeriy' successfully created.

这里强烈推荐使用淘宝镜像安装:

$ ng new helloKeriy --skip-install  // 先跳过npm安装
$ cd helloKeriy
$ cnpm install // 使用淘宝源安装

4、成果展示

安装完成之后就可以启动项目了:

cd helloKeriy
ng serve -open

ng serve命令会启动开发服务器,监听文件变化,并在修改这些文件时重新构建此应用。
使用--open(或-o)参数可以自动打开浏览器并访问http://localhost:4200/
接下来你将看到:

5、安装Angular Material

5.1、安装

官方安装说明:https://material.angular.io/guide/getting-started

npm install --save @angular/material @angular/cdk @angular/animations

然后执行修复

npm audit fix

5.2、配置animations

将BrowserAnimationsModule导入应用程序以启用动画支持。

import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

@NgModule({
...
imports: [BrowserAnimationsModule],
...
})
export class PizzaPartyAppModule { }

示例:打开app.module.ts并修改:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; @NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

5.3、导入组件模块(component modules)

import {MatButtonModule, MatCheckboxModule} from '@angular/material';

@NgModule({
...
imports: [MatButtonModule, MatCheckboxModule],
...
})
export class PizzaPartyAppModule { }

示例:打开app.module.ts并修改:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {MatButtonModule, MatCheckboxModule} from '@angular/material'; import { AppComponent } from './app.component'; @NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatButtonModule,
MatCheckboxModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

5.4、添加主题

包含主题是将所有核心和主题样式应用于您的应用程序所必需的。要开始使用预先构建的主题,请在应用程序中全局包含Angular Material的预构建主题之一。如果您正在使用Angular CLI,则可以将其添加到styles.css中。

如果您不使用Angular CLI,则可以通过 index.html 中的<link>元素包含预构建的主题。有关主题的更多信息以及有关如何创建自定义主题的说明,请参阅主题指南

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

5.5、添加手势支持(Gesture Support)

npm install --save hammerjs

然后在 src/main.ts 中引入

import 'hammerjs';

5.6、添加图标

如果您想将mat-icon组件与正式的材质设计图标一起使用,请在 index.html 中加载图标字体。

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Angular CLI 安装和使用的更多相关文章

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

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

  2. Angular CLI 安装

    安装Angular 官网的教程,因为国内网络环境原因,访问不了服务器,导致安装失败. 1.先安装NodeJs 安装教程:http://blog.csdn.net/zengmingen/article/ ...

  3. Angular/cli 安装(windows环境)。

    1.卸载先前安装的Angular/cli npm uninstall -g angular-clinpm uninstall --save-dev angular-clinpm uninstall - ...

  4. [转]Angular CLI 安装和使用

    本文转自:https://www.jianshu.com/p/327d88284abb 一. 背景介绍: 两个概念: 关于Angular版本,Angular官方已经统一命名Angular 1.x统称为 ...

  5. Angular CLI 安装和使用以及安装失败的解决方法

    背景介绍 关于Angular版本,Angular官方已经统一命名Angular 1.x同一为Angular JS:Angular 2.x及以上统称Angular: CLI是Command Line I ...

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

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

  7. Windows 安装Angular CLI

    1.安装nvm npm cnpm nrm(onenote笔记上有记录) 参考:https://blog.csdn.net/tyro_java/article/details/51232458 提示:如 ...

  8. Angular/cli的安装

    Angular cli 是一个命令行工具,用来创建,打包,发布项目. Angular cli 安装之前必须先安装Node 6.9.0及以上版本,NPM 3 及以上版本. 在cmd控制台窗口执行命令no ...

  9. angular cli全局版本大于本地版本 把本地版本升级方式

    查看 angular 版本  ng version 如出现提示 Your global Angular CLI version (xxx) is greater than your local ver ...

随机推荐

  1. [Poi2012]A Horrible Poem BZOJ2795

    分析: 这是今天下午的考试题,推了2个小时,考试中A掉了 首先,循环串通过字符串hash可以O(1)判断:get_hash(l,r-len)==get_hash(l+len,r);显然可证. 我们其次 ...

  2. BZOJ_3894_文理分科&&BZOJ_2127_happiness_最小割

    BZOJ_3894_文理分科_最小割 Description  文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过)  小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进 ...

  3. b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子图

    b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子 题意:n*m个植物,每个植物有分数(可正可负),和能保护植物的位置.只能从右往左吃,并且不能吃正被保护着的,可以一个不吃,求 ...

  4. BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP

    BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是 ...

  5. 虚拟机console基础环境配置——sshd安全登陆

    1. 概述2. 配置console的登陆2.1 配置sshd服务2.2 重启sshd服务2.3 无法登陆的问题解决3. 配置密钥登陆3.1 上传公钥的方式3.2 下载密码的方式3.3 虚拟机和宿主机共 ...

  6. c语言最大公约数及最小公倍数的详解

    今天我打算把,学习到的一些知识整理一下,方便给以后的学弟学妹做一个参考! 这一次是关于最大公约数和最小公倍数的知识:这是百度关于最大公约数的介绍 感谢我的一位学姐的博文,让我能够更快的明白! 求最小公 ...

  7. selenium+python+eclipse 实现 “问卷星”网站,登录与检查登录示例!

    1.使用selenium+python+eclipse实现的登录"问卷星",问卷星访问地址:https://www.sojump.com/ 2.实现步骤:1)进入链接---首页-- ...

  8. gorm的日志模块源码解析

    gorm的日志模块源码解析 如何让gorm的日志按照我的格式进行输出 这个问题是<如何为gorm日志加traceId>之后,一个群里的朋友问我的.如何让gorm的sql日志不打印到控制台, ...

  9. Java核心技术梳理-集合

    一.前言 在日常开发中,我们经常会碰到需要在运行时才知道对象个数的情况,这种情况不能使用数组,因为数组是固定数量的,这个时候我们就会使用集合,因为集合可以存储数量不确定的对象. 集合类是特别有用的工具 ...

  10. 异步多线程 Async

    进程:进程是一个程序在电脑运行时,全部资源的合集叫进程 线程:是程序的最小执行单位,包含计算资源,任何一个操作的响应都是线程完成的.   多线程:多个线程并发执行   Thread 是.net框架封装 ...