Angular CLI 是 Angular 客戶端命令行工具,提供非常多的命令來簡化 Angular 的開發。 本文總結了在實際項目中經常會用到的 Angular CLI 命令。
獲取幫助(ng -h
)
ng -h
等同於ng --help
,跟所有的其他命令行一樣,用於查看所有命令的一個幫助命令。執行該命令可以看到 Angular CLI 所有的命令:
>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 應用:
ng new user-management
創建組件
以下示例,創建一個名爲 UsersComponent 的組件:
ng generate component users
創建服務
以下示例,創建一個名爲 UserService 的服務:
ng generate service user
啓動應用
執行:
ng serve --open
此時,應用就會自動在瀏覽器中打開。訪問地址爲 http://localhost:4200/。
升級依賴
目前,Angular 社區非常活躍,版本會經常更新。對 Angular 的版本做升級,只需簡單一步執行:
ng update
如果是想把整個應用的依賴都升級,則執行:
ng update --all
自動化測試
Angular 支持自動化測試。Angular的測試,主要是基於Jasmine和Karma庫來實現的。只需簡單一步執行:
ng test
要生成覆蓋率報告,運行下列命令:
ng test --code-coverage
下載依賴
光有 Angular 源碼是否不足以將 Angular 啓動起來的,需要先安裝 Angular 應用所需要的依賴到本地。
在應用目錄下執行:
npm install
參考引用
- 更多有關 Angular 的內容,可以參閱《跟老衛學Angular》:https://github.com/waylau/angular-tutorial
- 原文同步至:https://waylau.com/angular-cli-commands/