Angular CLI 經常使用命令

Angular CLI 是 Angular 客戶端命令行工具,提供很是多的命令來簡化 Angular 的開發。 本文總結了在實際項目中常常會用到的 Angular CLI 命令。git

Angular CLI

獲取幫助(ng -h

ng -h等同於ng --help,跟全部的其餘命令行同樣,用於查看全部命令的一個幫助命令。執行該命令能夠看到 Angular CLI 全部的命令:github

>ng -h
Available Commands:
  add Adds support for an external library to your project.
  build (b) Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.
  config Retrieves or sets Angular configuration values.
  doc (d) Opens the official Angular documentation (angular.io) in a browser, and searches for a given keyword.
  e2e (e) Builds and serves an Angular app, then runs end-to-end tests using Protractor.
  generate (g) Generates and/or modifies files based on a schematic.
  help Lists available commands and their short descriptions.
  lint (l) Runs linting tools on Angular app code in a given project folder.
  new (n) Creates a new workspace and an initial Angular app.
  run Runs a custom target defined in your project.
  serve (s) Builds and serves your app, rebuilding on file changes.
  test (t) Runs unit tests in a project.
  update Updates your application and its dependencies. See https://update.angular.io/
  version (v) Outputs Angular CLI version.
  xi18n Extracts i18n messages from source code.

For more detailed help run "ng [command name] --help"

建立應用

如下示例,建立一個名爲「user-management」的 Angular 應用:npm

ng new user-management

建立組件

如下示例,建立一個名爲 UsersComponent 的組件:瀏覽器

ng generate component users

建立服務

如下示例,建立一個名爲 UserService 的服務:app

ng generate service user

啓動應用

執行:工具

ng serve --open

此時,應用就會自動在瀏覽器中打開。訪問地址爲 http://localhost:4200/。測試

升級依賴

目前,Angular 社區很是活躍,版本會常常更新。對 Angular 的版本作升級,只需簡單一步執行:ui

ng update

若是是想把整個應用的依賴都升級,則執行:spa

ng update --all

自動化測試

Angular 支持自動化測試。Angular的測試,主要是基於Jasmine和Karma庫來實現的。只需簡單一步執行:命令行

ng test

要生成覆蓋率報告,運行下列命令:

ng test --code-coverage

下載依賴

光有 Angular 源碼是否不足以將 Angular 啓動起來的,須要先安裝 Angular 應用所須要的依賴到本地。

在應用目錄下執行:

npm install

參考引用

相關文章
相關標籤/搜索