開發 Angular 就不能不知道 Angular CLI 這個超級好用的命令列工具,有了這個工具,原本渾沌的開發環境,頓時清晰,許多繁瑣的瑣事,一個命令就搞定!這邊是我自己的操作筆記,讓記憶力不佳的我,有個地方可以方便查詢。
這裡的內容不保證最新,但會盡量持續更新。
最即時的文件請參考 Angular CLI 在 Github 上的文件,例如要查
ng new
這個指令的用法,請參考這個連結。
建議使用 Node.js 的 npm 套件管理工具來安裝 Angular CLI,請使用以下指令安裝:
Angular CLI 需要 Node 4.X 和 NPM 3.X 以上的版本支援。
ng new AppName --routing --skip-tests --style=scss -d
ng new AppName --routing --skip-tests --style=scss
ng new AppName --routing --skip-tests --style=scss --service-worker
ng serve
參考:簡介 Angular 4 如何升級至 Angular 5 最新版本
ng -v
查詢目前 Angular CLI 版本npm show @angular/core@* version --json
查詢 Angular 版本package.json
中 所有 @angular/*
套件版本至最新版,例 ^5.0.0
npm install @angular/cli@1.5
npm install typescript@2.4
npm install rxjs@5.5
npm install codelyzer@4
npm install
重新安裝套件指令:ng new <project-name> [options]
說明:建立 Angular 專案,預設此專案會建立在目前的路徑下
選項:
-d
-sg
-si
-st
-se
--directory ./
-v
-sw
指令:ng init <project-name> [options]
說明:在目前資料夾下建立 Angular 專案(不會產生資料夾)
選項:
-d
-v
在專案資料夾中執行 ng serve
,將編譯 Angular 專案並自動在瀏覽器中打開預設網址 http://localhost:4200/
,執行後如果修改了專案中的程式碼,網頁會自動重新整理。
也可以使用以下指令,自訂要配置的 IP 和 Port 號:
ng completion
ng doc <keyword>
<keyword>
用法。相當於在 angular.io API Reference 中查詢 API 文件。ng e2e
ng format
ng generate <type> [options]
g
ng g component my-new-component
產生 Component 元件程式碼ng g directive my-new-directive
產生 Directive 指令程式碼ng g pipe my-new-pipe
產生 Pipe 管道程式碼ng g service my-new-service
產生 Service 服務程式碼ng g class my-new-class
產生 Class 程式碼ng g interface my-new-interface
產生 Interface 介面程式碼ng g enum my-new-enum
產生 Enum 程式碼ng g module my-module
產生 Module 模組程式碼ng g guard my-guard
產生 Guard 守衛程式碼ng g app-shell [ --universal-app <universal-app-name>] [ --route <route>]
建立 App Shell每個產生的元件有各自的資料夾,除非使用 --flat
選項
--flat
不建立資料夾--route=<route>
指令父路由,僅用於產生元件和路由,預設使用指定的路徑--skip-router-generation
跳過產生父路由配置,只能用於路由命令--default
指定路由為預設路由 * --lazy
指定路由為延遲載入將專案編譯至輸出資料夾,預設為 dist
。
ng build
可以指定輸出目標(--target=production
或 --target=development
)和要使用的環境文件(--environment=dev
或 --environment=prod
)。預設情況下,會使用開發目標和環境。
ng get <path1, path2, ...pathN> [options]
--global
Returns the global configuration value instead of the local one (if both are set). This option also makes the command work outside of a project directory.ng get <path1=value1, path2=value2, ...pathN=valueN> [options]
--global
Sets the global configuration value instead of a local one. This also makes ng set
works outside a project.ng github-pages:deploy [options]
--message=<message>
Commit message to include with the build. Defaults to “new gh-pages version”.--environment=<env>
The Angular environment to build. Defaults to “production”.--branch=<branch-name>
The git branch to push the pages to. Defaults to “gh-branch”.--skip-build
Skip building the project before publishing.--gh-token=<token>
API token to use to deploy. Required.--gh-username=<username>
The Github username to use. Required.ng lint
ng test [options]
--watch
Keep running the tests. Default to true.--browsers
, --colors
, --reporters
, --port
, --log-level
Those arguments are passed directly to karma.ng version