Angular CLI 安裝和使用


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">

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM