填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

工作區和專案檔案結構

Workspace and project file structure

你會以 Angular 工作空間作為上下文來開發應用。工作空間包含一個或多個專案的檔案集。一個專案就是一組包含獨立應用或可共享函式庫的檔案。

You develop applications in the context of an Angular workspace. A workspace contains the files for one or more projects. A project is the set of files that comprise a standalone application or a shareable library.

Angular CLI 的 ng new 命令可以建立一個工作空間。

The Angular CLI ng new command creates a workspace.

ng new <my-project>
      
      ng new <my-project>
    

當你執行這個命令時,CLI 會在一個新的工作區中安裝必需的 Angular npm 套件和其它依賴項,其根應用名叫 my-project。 該工作空間的根資料夾中包含一些工作空間配置檔案,和一個帶有自動產生的描述性文字的自述檔案,你可以自訂它。

When you run this command, the CLI installs the necessary Angular npm packages and other dependencies in a new workspace, with a root-level application named my-project. The workspace root folder contains various support and configuration files, and a README file with generated descriptive text that you can customize.

ng new 還會預設建立一個位於工作空間根級的骨架應用,及其端到端測試專案。這個骨架是一個簡單的 Welcome 應用,它可以執行,也很容易修改。這個根應用與工作空間同名,其原始檔位於工作空間的 src/ 子資料夾中。

By default, ng new creates an initial skeleton application at the root level of the workspace, along with its end-to-end tests. The skeleton is for a simple Welcome application that is ready to run and easy to modify. The root-level application has the same name as the workspace, and the source files reside in the src/ subfolder of the workspace.

這種預設行為適用於典型的“多重(multi)儲存庫”開發風格,每個應用都位於它自己的工作空間中。建議初學者和中級使用者使用 ng new 為每個應用建立一個單獨的工作空間。

This default behavior is suitable for a typical "multi-repo" development style where each application resides in its own workspace. Beginners and intermediate users are encouraged to use ng new to create a separate workspace for each application.

Angular 還支援包含多個專案的工作空間。這種開發環境適用於正在開發可共享函式庫的高階使用者,以及那些使用“單一(mono)儲存庫”開發風格的企業,它只需要一個儲存庫,而且所有 Angular 專案都使用全域性配置。

Angular also supports workspaces with multiple projects. This type of development environment is suitable for advanced users who are developing shareable libraries, and for enterprises that use a "monorepo" development style, with a single repository and global configuration for all Angular projects.

要設定單一儲存庫的工作空間,你應該跳過建立根應用的過程。請參閱下面的設定多專案工作空間部分。

To set up a monorepo workspace, you should skip the creating the root application. See Setting up for a multi-project workspace below.

工作區配置檔案

Workspace configuration files

每個工作空間中的所有專案共享同一個 CLI 配置環境。該工作空間的最上層包含著全工作空間級的配置檔案、根應用的配置檔案以及一些包含根應用的原始檔和測試檔案的子資料夾。

All projects within a workspace share a CLI configuration context. The top level of the workspace contains workspace-wide configuration files, configuration files for the root-level application, and subfolders for the root-level application source and test files.

工作空間配置檔案

WORKSPACE CONFIG FILES

用途

PURPOSE

.editorconfig

程式碼編輯器的配置。參閱 EditorConfig

Configuration for code editors. See EditorConfig.

.gitignore

指定 Git 應忽略的不必追蹤的檔案。

Specifies intentionally untracked files that Git should ignore.

README.md

根應用的簡介文件.

Introductory documentation for the root app.

angular.json

為工作區中的所有專案指定 CLI 的預設配置,包括 CLI 要用到的建構、啟動開發伺服器和測試工具的配置項,比如 TSLintKarmaProtractor。欲知詳情,請參閱 Angular 工作空間配置 部分。

CLI configuration defaults for all projects in the workspace, including configuration options for build, serve, and test tools that the CLI uses, such as TSLint, Karma, and Protractor. For details, see Angular Workspace Configuration.

package.json

配置工作空間中所有專案可用的 npm 套件相依。關於此檔案的具體格式和內容,請參閱 npm 的文件

Configures npm package dependencies that are available to all projects in the workspace. See npm documentation for the specific format and contents of this file.

package-lock.json

提供 npm 客戶端安裝到 node_modules 的所有軟體套件的版本資訊。欲知詳情,請參閱 npm 的文件。如果你使用的是 yarn 客戶端,那麼該檔案就是 yarn.lock

Provides version information for all packages installed into node_modules by the npm client. See npm documentation for details. If you use the yarn client, this file will be yarn.lock instead.

src/

根專案的原始檔。

Source files for the root-level application project.

node_modules/

為整個工作空間提供 npm 套件。這些工作空間級的 node_modules 依賴對其中的所有專案可見。

Provides npm packages to the entire workspace. Workspace-wide node_modules dependencies are visible to all projects.

tsconfig.json

工作空間中所有專案的基本 TypeScript 配置。所有其它配置檔案都繼承自這個基本配置。欲知詳情,參閱 TypeScript 文件中的 透過 extends 進行配置繼承 部分。

The base TypeScript configuration for projects in the workspace. All other configuration files inherit from this base file. For more information, see the Configuration inheritance with extends section of the TypeScript documentation.

tslint.json

工作空間中所有專案的預設的 TSLint 配置。

Default TSLint configuration for projects in the workspace.

應用專案檔案

Application project files

CLI 命令 ng new my-app 會預設建立名為 “my-app” 的工作空間資料夾,並在 src/ 資料夾下為工作空間最上層的根應用產生一個新的應用骨架。新產生的應用包含一個根模組的原始檔,包括一個根元件及其範本。

By default, the CLI command ng new my-app creates a workspace folder named "my-app" and generates a new application skeleton in a src/ folder at the top level of the workspace. A newly generated application contains source files for a root module, with a root component and template.

當工作空間檔案結構到位時,可以在命令列中使用 ng generate 命令往該應用中新增功能和資料。這個初始的根應用是 CLI 命令的預設應用(除非你在建立其它應用之後更改了預設值)。

When the workspace file structure is in place, you can use the ng generate command on the command line to add functionality and data to the application. This initial root-level application is the default app for CLI commands (unless you change the default after creating additional apps).

除了在命令列中使用 CLI 之外,你還可以使用像 Angular Console 這樣的互動式開發環境,或直接在應用的原始檔夾和配置檔案中操作這些檔案。

Besides using the CLI on the command line, you can also manipulate files directly in the app's source folder and configuration files.

對於單應用的工作區,工作空間的 src/ 子資料夾包含根應用的原始檔(應用邏輯、資料和靜態資源)。對於多專案的工作空間,projects/ 資料夾中的其它專案各自包含一個具有相同結構的 project-name/src/ 子目錄。

For a single-application workspace, the src/ subfolder of the workspace contains the source files (application logic, data, and assets) for the root application. For a multi-project workspace, additional projects in the projects/ folder contain a project-name/src/ subfolder with the same structure.

應用原始檔

Application source files

最上層檔案 src/ 為測試並執行你的應用提供支援。其子資料夾中包含應用原始碼和應用的專屬配置。

Files at the top level of src/ support testing and running your application. Subfolders contain the application source and application-specific configuration.

應用支援檔案

APP SUPPORT FILES

目的

PURPOSE

app/

包含定義應用邏輯和資料的元件檔案。詳見下文

Contains the component files in which your application logic and data are defined. See details below.

assets/

包含要在建構應用時應該按原樣複製的影象和其它靜態資原始檔。

Contains image and other asset files to be copied as-is when you build your application.

environments/

包含特定目標環境的建構配置選項。預設情況下,有一個無名的標準開發環境和一個生產(“prod”)環境。你還可以定義其它的目標環境配置。

Contains build configuration options for particular target environments. By default there is an unnamed standard development environment and a production ("prod") environment. You can define additional target environment configurations.

favicon.ico

用作該應用在標籤欄中的圖示。

An icon to use for this application in the bookmark bar.

index.html

當有人訪問你的站點時,提供服務的主要 HTML 頁面。CLI 會在建構你的應用時自動新增所有的 JavaScript 和 CSS 檔案,所以你通常不用手動新增任何 <script><link> 標籤。

The main HTML page that is served when someone visits your site. The CLI automatically adds all JavaScript and CSS files when building your app, so you typically don't need to add any <script> or<link> tags here manually.

main.ts

應用的主要切入點。用 JIT 編譯器編譯應用,然後引導應用的根模組(AppModule)在瀏覽器中執行。你也可以在不改變任何程式碼的情況下改用 AOT 編譯器,只要在 CLI 的 buildserve 命令中加上 --aot 標誌就可以了。

The main entry point for your application. Compiles the application with the JIT compiler and bootstraps the application's root module (AppModule) to run in the browser. You can also use the AOT compiler without changing any code by appending the --aot flag to the CLI build and serve commands.

polyfills.ts

為瀏覽器支援提供了Polyfill(polyfill)指令碼。

Provides polyfill scripts for browser support.

styles.sass

列出為專案提供樣式的 CSS 檔案。該擴充套件還反映了你為該專案配置的樣式前處理器。

Lists CSS files that supply styles for a project. The extension reflects the style preprocessor you have configured for the project.

test.ts

單元測試的主入口點,帶有一些 Angular 特有的配置。你通常不需要編輯這個檔案。

The main entry point for your unit tests, with some Angular-specific configuration. You don't typically need to edit this file.

如果你使用 Angular 的嚴格模式建立了應用,就會在 src/app 目錄下有一個額外的 package.json 檔案。欲知詳情,請參閱嚴格模式

If you create an application using Angular's strict mode, you will also have an additional package.json file in the src/app directory. For more information, see Strict mode.

src/ 資料夾裡面,app/ 資料夾中包含此專案的邏輯和資料。Angular 元件、範本和樣式也都在這裡。

Inside the src/ folder, the app/ folder contains your project's logic and data. Angular components, templates, and styles go here.

src/app/ 檔案

src/app/ FILES

用途

PURPOSE

app/app.component.ts

為應用的根元件定義邏輯,名為 AppComponent。當你嚮應用中新增元件和服務時,與這個根元件相關聯的檢視就會成為檢視樹的根。

Defines the logic for the app's root component, named AppComponent. The view associated with this root component becomes the root of the view hierarchy as you add components and services to your application.

app/app.component.html

定義與根元件 AppComponent 關聯的 HTML 範本。

Defines the HTML template associated with the root AppComponent.

app/app.component.css

為根元件 AppComponent 定義了基本的 CSS 樣式表。

Defines the base CSS stylesheet for the root AppComponent.

app/app.component.spec.ts

為根元件 AppComponent 定義了一個單元測試。

Defines a unit test for the root AppComponent.

app/app.module.ts

定義了名為 AppModule 的根模組,它會告訴 Angular 如何組裝應用。這裡最初只宣告一個 AppComponent。當你嚮應用中新增更多元件時,它們也必須在這裡宣告。

Defines the root module, named AppModule, that tells Angular how to assemble the application. Initially declares only the AppComponent. As you add more components to the app, they must be declared here.

應用配置檔案

Application configuration files

根應用的配置檔案位於工作空間的根目錄下。對於多專案工作空間,專案專屬的配置檔案位於專案根目錄 projects/project-name/

The application-specific configuration files for the root application reside at the workspace root level. For a multi-project workspace, project-specific configuration files are in the project root, under projects/project-name/.

專案專屬的 TypeScript 配置檔案繼承自工作區範圍內的 tsconfig.base.json,而專案專屬的 TSLint 配置檔案則繼承自全工作區級內的 tslint.json

Project-specific TypeScript configuration files inherit from the workspace-wide tsconfig.json, and project-specific TSLint configuration files inherit from the workspace-wide tslint.json.

應用專屬的配置檔案

APPLICATION-SPECIFIC CONFIG FILES

用途

PURPOSE

.browserslistrc

browserslist

配置各種前端工具之間共享的目標瀏覽器和 Node.js 版本。欲知詳情,請參閱 GitHub 上的瀏覽器列表

Configures sharing of target browsers and Node.js versions among various front-end tools. See Browserslist on GitHub for more information.

karma.conf.js

應用專屬的 Karma 配置。

Application-specific Karma configuration.

tsconfig.app.json

應用專屬的 TypeScript 配置,包括 TypeScript 和 Angular 範本編譯器的選項。參閱 TypeScript 配置

Application-specific TypeScript configuration, including TypeScript and Angular template compiler options. See TypeScript Configuration and Angular Compiler Options.

tsconfig.spec.json

應用測試的 TypeScript 配置。參閱 TypeScript 配置

TypeScript configuration for the application tests. See TypeScript Configuration.

tslint.json

應用專屬的 TSLint 配置。

Application-specific TSLint configuration.

端到端測試檔案

End-to-end test files

根級的 e2e/ 資料夾中包含一組針對根應用的端到端測試的原始檔,以及測試專屬的配置檔案。

An e2e/ folder at the top level contains source files for a set of end-to-end tests that correspond to the root-level application, along with test-specific configuration files.

對於多專案的工作空間,應用專屬的端到端測試檔案都位於專案各自的根目錄下,即 projects/project-name/e2e/

For a multi-project workspace, application-specific end-to-end tests are in the project root, under projects/project-name/e2e/.

e2e/ src/ (end-to-end tests for my-app) app.e2e-spec.ts app.po.ts protractor.conf.js (test-tool config) tsconfig.json (TypeScript config inherits from workspace)
      
      e2e/
   src/                 (end-to-end tests for my-app)
      app.e2e-spec.ts
      app.po.ts
    protractor.conf.js  (test-tool config)
    tsconfig.json       (TypeScript config inherits from workspace)
    

多重專案

Multiple projects

多專案工作空間適用於對所有 Angular 專案使用單一儲存函式庫(單儲存庫模型)和全域性配置的企業。多專案工作空間也能為函式庫開發提供支援。

A multi-project workspace is suitable for an enterprise that uses a single repository and global configuration for all Angular projects (the "monorepo" model). A multi-project workspace also supports library development.

建立多專案工作空間

Setting up for a multi-project workspace

如果你打算在工作區中包含多個專案,可以在建立工作空間時不要自動建立初始應用,並為工作空間指定一個唯一的名字。下列命令用於建立一個包含全工作空間級配置檔案的工作空間,但沒有根應用。

If you intend to have multiple projects in a workspace, you can skip the initial application generation when you create the workspace, and give the workspace a unique name. The following command creates a workspace with all of the workspace-wide configuration files, but no root-level application.

ng new my-workspace --createApplication="false"
      
      ng new my-workspace --createApplication="false"
    

然後,你可以使用工作空間內唯一的名字來產生應用和函式庫。

You can then generate apps and libraries with names that are unique within the workspace.

cd my-workspace ng generate application my-first-app
      
      cd my-workspace
ng generate application my-first-app
    

多重專案的檔案結構

Multiple project file structure

工作空間中第一個顯式產生的應用會像工作空間中的其它專案一樣放在 projects/ 資料夾中。新產生的函式庫也會新增到 projects/ 下。當你以這種方式建立專案時,工作空間的檔案結構與工作空間配置檔案 angular.json 中的結構完全一致。

The first explicitly generated application goes into the projects/ folder along with all other projects in the workspace. Newly generated libraries are also added under projects/. When you create projects this way, the file structure of the workspace is entirely consistent with the structure of the workspace configuration file, angular.json.

my-workspace/ ... (workspace-wide config files) projects/ (generated applications and libraries) my-first-app/ --(an explicitly generated application) ... --(application-specific config) e2e/ ----(corresponding e2e tests) src/ ----(e2e tests source) ... ----(e2e-specific config) src/ --(source and support files for application) my-lib/ --(a generated library) ... --(library-specific config) src/ --source and support files for library)
      
      my-workspace/
  ...             (workspace-wide config files)
  projects/       (generated applications and libraries)
    my-first-app/ --(an explicitly generated application)
      ...         --(application-specific config)
      e2e/        ----(corresponding e2e tests)
         src/     ----(e2e tests source)
         ...      ----(e2e-specific config)
      src/        --(source and support files for application)
    my-lib/       --(a generated library)
      ...         --(library-specific config)
      src/        --source and support files for library)
    

函式庫專案檔案

Library project files

當你使用 CLI 命令(例如 ng generate library my-lib)產生一個函式庫時,所產生的檔案會放在工作區的 projects/ 資料夾中。關於如何建立自己的函式庫的更多資訊,請參閱建立函式庫一章。

When you generate a library using the CLI (with a command such as ng generate library my-lib), the generated files go into the projects/ folder of the workspace. For more information about creating your own libraries, see Creating Libraries.

與應用及其相關的 e2e 專案不同,函式庫有它自己的 package.json 配置檔案。

Libraries (unlike applications and their associated e2e projects) have their own package.json configuration files.

projects/ 目錄下,my-lib 資料夾中包含你的函式庫程式碼。

Under the projects/ folder, the my-lib folder contains your library code.

函式庫的原始檔

LIBRARY SOURCE FILES

用途

PURPOSE

src/lib

包含函式庫專案的邏輯和資料。像應用專案一樣,函式庫專案也可以包含元件、服務、模組、指令和管道。

Contains your library project's logic and data. Like an application project, a library project can contain components, services, modules, directives, and pipes.

src/test.ts

單元測試的主要入口點是一些函式庫專屬的配置。你通常不用編輯這個檔案。

The main entry point for your unit tests, with some library-specific configuration. You don't typically need to edit this file.

src/public-api.ts

指定從函式庫中匯出的所有檔案。

Specifies all files that are exported from your library.

karma.conf.js

函式庫專屬的 Karma 配置。

Library-specific Karma configuration.

ng-package.json

建構函式庫時,ng-packagr 用到的配置檔案。

Configuration file used by ng-packagr for building your library.

package.json

配置這個函式庫所需的 npm 套件相依

Configures npm package dependencies that are required for this library.

tsconfig.lib.json

函式庫專屬的 TypeScript 配置,包括 TypeScript 和 Angular 範本編譯器選項。參閱 TypeScript 配置

Library-specific TypeScript configuration, including TypeScript and Angular template compiler options. See TypeScript Configuration.

tsconfig.spec.json

測試函式庫時用到的 TypeScript 配置。參閱 TypeScript 配置

TypeScript configuration for the library tests. See TypeScript Configuration.

tslint.json

函式庫專屬的 TSLint 配置。

Library-specific TSLint configuration.