41 KiB
Workspace and project file structure
工作区和项目文件结构
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 工作空间作为上下文来开发应用。工作空间包含一个或多个项目的文件集。一个项目就是一组包含独立应用或可共享库的文件。
The Angular CLI ng new
command creates a workspace.
Angular CLI 的 ng new
命令可以创建一个工作空间。
When you run this command, the CLI installs the necessary Angular npm packages and other dependencies in a new workspace, with a root folder named project_name. The workspace root folder contains workspace configuration files and a README file with generated descriptive text that you can customize.
当你运行这个命令时,CLI 会在一个新的工作区中安装必需的 Angular npm 包和其它依赖项,其根文件夹名叫 project_name。该工作空间的根文件夹中包含一些工作空间配置文件,和一个带有自动生成的描述性文本的自述文件,你可以自定义它。
By default, ng new
also creates an initial skeleton application, along with its end-to-end tests.
The skeleton is for a simple Welcome application that is ready to run and easy to modify.
This root application has the same name as the workspace, and the source files reside in the src/
subfolder of the workspace.
ng new
还会默认创建一个初始的骨架应用,以及它的端到端测试项目。这个骨架是一个简单的 Welcome 应用,它可以运行,也很容易修改。这个根应用与工作空间同名,其源文件位于工作空间的 src/
子文件夹中。
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.
这种默认行为适用于典型的“多重(multi)仓库”开发风格,每个应用都位于它自己的工作空间中。建议初学者和中级用户使用 ng new
为每个应用创建一个单独的工作空间。
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.
Angular 还支持包含多个项目的工作空间。这种开发环境适用于正在开发可共享库的高级用户,以及那些使用“单一(mono)仓库”开发风格的企业,它只需要一个仓库,而且所有 Angular 项目都使用全局配置。
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
工作区配置文件
All projects within a workspace share a CLI configuration context. The top level of the workspace contains workspace-wide configuration files.
每个工作空间中的所有项目共享同一个 CLI 配置环境 。该工作空间的顶层包含着全工作空间级的配置文件。
WORKSPACE CONFIG FILES | PURPOSE |
---|---|
工作空间配置文件 | 用途 |
node_modules/ |
Provides npm packages to the entire workspace. Workspace-wide node_modules dependencies are visible to all projects. |
node_modules/ |
为整个工作空间提供了 npm 包 。所有项目都可以看到工作区范围内的 node_modules 依赖项。 |
.editorconfig |
Configuration for code editors. See EditorConfig. |
.editorconfig |
代码编辑器的配置。参见 EditorConfig 。 |
.gitignore |
Specifies intentionally untracked files that Git should ignore. |
.gitignore |
指定 Git 应忽略的不必追踪的文件。 |
angular.json |
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. |
angular.json |
为工作区中的所有项目指定 CLI 的默认配置,包括 CLI 要用到的构建、启动开发服务器和测试工具的配置项,比如 TSLint,Karma 和 Protractor。欲知详情,请参阅 Angular 工作空间配置 部分。 |
package.json |
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.json |
配置工作空间中所有项目可用的 npm包依赖 。有关此文件的具体格式和内容,请参阅 npm 的文档 。 |
package-lock.json |
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. |
package-lock.json |
提供 npm 客户端安装到 node_modules 的所有软件包的版本信息。欲知详情,请参阅 npm 的文档。如果你使用的是 yarn 客户端,那么该文件就是 yarn.lock 。 |
README.md |
Introductory documentation for the root app. |
README.md |
根应用的介绍性文档。 |
tsconfig.json |
Default TypeScript configuration for projects in the workspace. |
tsconfig.json |
工作空间中各个项目的默认 TypeScript 配置。 |
tslint.json |
Default TSLint configuration for projects in the workspace. |
tslint.json |
工作空间中各个项目的默认 TSLint 配置。 |
Application project files
应用项目文件
By default, the CLI command ng new my-app
creates a workspace folder named "my-app" and generates a new application skeleton for a root application at the top level of the workspace.
A newly generated application contains source files for a root module, with a root component and template.
CLI 命令ng new my-app
会默认创建名为 “my-app” 的工作空间文件夹,并为工作空间顶层的根应用生成一个新的应用骨架。新生成的应用包含一个根模块的源文件,包括一个根组件及其模板。
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 starter application is the default app for CLI commands (unless you change the default after creating additional apps).
当工作空间文件结构到位时,可以在命令行中使用 ng generate
命令往该应用中添加功能和数据。这个初始的起步者应用是 CLI 命令的默认应用(除非你在创建其它应用之后更改了默认值)。
Besides using the CLI on the command line, you can also use an interactive development environment like Angular Console, or manipulate files directly in the app's source folder and configuration files.
除了在命令行中使用 CLI 之外,你还可以使用像 Angular Console 这样的交互式开发环境,或直接在应用的源文件夹和配置文件中操作这些文件。
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.
对于单应用的工作区,工作空间的 src/
子文件夹包含根应用的源文件(应用逻辑、数据和静态资源)。对于多项目的工作空间,projects/
文件夹中的其它项目各自包含一个具有相同结构的 project-name/src/
子目录。
Application source files
应用源文件
Files at the top level of src/
support testing and running your application. Subfolders contain the application source and application-specific configuration.
顶级文件 src/
为测试并运行你的应用提供支持。其子文件夹中包含应用源代码和应用的专属配置。
APP SUPPORT FILES | PURPOSE |
---|---|
应用支持文件 | 目的 |
app/ |
Contains the component files in which your application logic and data are defined. See details below. |
app/ |
包含定义应用逻辑和数据的组件文件。详见下文 。 |
assets/ |
Contains image and other asset files to be copied as-is when you build your application. |
assets/ |
包含要在构建应用时应该按原样复制的图像和其它静态资源文件。 |
environments/ |
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. |
environments/ |
包含特定目标环境的构建配置选项。默认情况下,有一个无名的标准开发环境和一个生产(“prod”)环境。你还可以定义其它的目标环境配置。 |
favicon.ico |
An icon to use for this application in the bookmark bar. |
favicon.ico |
用作该应用在标签栏中的图标。 |
index.html |
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. |
index.html |
当有人访问你的站点时,提供服务的主要 HTML 页面。CLI 会在构建你的应用时自动添加所有的 JavaScript 和 CSS 文件,所以你通常不用手动添加任何 <script> 或 <link> 标签。 |
main.ts |
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. |
main.ts |
应用的主要切入点。用 JIT 编译器编译应用,然后引导应用的根模块(AppModule)在浏览器中运行。你也可以在不改变任何代码的情况下改用 AOT 编译器, 只要在 CLI 的 build 和 serve 命令中加上 --aot 标志就可以了。 |
polyfills.ts |
Provides polyfill scripts for browser support. |
polyfills.ts |
为浏览器支持提供了腻子(polyfill)脚本。 |
styles.sass |
Lists CSS files that supply styles for a project. The extension reflects the style preprocessor you have configured for the project. |
styles.sass |
列出为项目提供样式的 CSS 文件。该扩展还反映了你为该项目配置的样式预处理器。 |
test.ts |
The main entry point for your unit tests, with some Angular-specific configuration. You don't typically need to edit this file. |
test.ts |
单元测试的主入口点,带有一些Angular特有的配置。你通常不需要编辑这个文件。 |
{@a app-src}
Inside the src/
folder, the app/
folder contains your project's logic and data.
Angular components, templates, and styles go here.
在 src/
文件夹里面, app/
文件夹中包含此项目的逻辑和数据。 Angular 组件、模板和样式也都在这里。
src/app/ FILES |
PURPOSE |
---|---|
src/app/ 文件 |
用途 |
app/app.component.ts |
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.ts |
为应用的根组件定义逻辑,名为 AppComponent 。当你向应用中添加组件和服务时,与这个根组件相关联的视图就会成为视图树的根。 |
app/app.component.html |
Defines the HTML template associated with the root AppComponent . |
app/app.component.html |
定义与根组件 AppComponent 关联的 HTML 模板。 |
app/app.component.css |
Defines the base CSS stylesheet for the root AppComponent . |
app/app.component.css |
为根组件 AppComponent 定义了基本的 CSS 样式表。 |
app/app.component.spec.ts |
Defines a unit test for the root AppComponent . |
app/app.component.spec.ts |
为根组件 AppComponent 定义了一个单元测试。 |
app/app.module.ts |
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. |
app/app.module.ts |
定义了名为 AppModule 的根模块,它会告诉 Angular 如何组装应用。这里最初只声明一个 AppComponent 。当你向应用中添加更多组件时,它们也必须在这里声明。 |
Application configuration files
应用配置文件
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/
.
根应用的配置文件位于工作空间的根目录下。对于多项目工作空间,项目专属的配置文件位于项目根目录 projects/project-name/
。
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
.
项目专属的 TypeScript 配置文件继承自工作区范围内的 tsconfig.json
,而项目专属的 TSLint 配置文件则继承自全工作区级内的 tslint.json
。
APPLICATION-SPECIFIC CONFIG FILES | PURPOSE |
---|---|
应用专属的配置文件 | 用途 |
browserslist |
Configures sharing of target browsers and Node.js versions among various front-end tools. See Browserslist on GitHub for more information. |
browserslist |
配置各种前端工具之间共享的目标浏览器和 Node.js 版本。欲知详情,请参阅 GitHub 上的浏览器列表 。 |
karma.conf.js |
Application-specific Karma configuration. |
karma.conf.js |
应用专属的 Karma 配置。 |
tsconfig.app.json |
Application-specific TypeScript configuration, including TypeScript and Angular template compiler options. See TypeScript Configuration. |
tsconfig.app.json |
应用专属的 TypeScript 配置,包括 TypeScript 和 Angular 模板编译器的选项。参见 TypeScript 配置 。 |
tsconfig.spec.json |
TypeScript configuration for the application tests. See TypeScript Configuration. |
tsconfig.spec.json |
应用测试的 TypeScript 配置。参见 TypeScript 配置 。 |
tslint.json |
Application-specific TSLint configuration. |
tslint.json |
应用专属的 TSLint 配置。 |
End-to-end test files
端到端测试文件
An e2e/
subfolder contains source files for a set of end-to-end tests that correspond to an application, along with test-specific configuration files.
e2e/
子文件夹包含一组和应用对应的端到端测试的源文件,以及测试专属的配置文件。
{@a multiple-projects}
Multiple projects
多重项目
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.
多项目工作空间适用于对所有 Angular 项目使用单一存储库(单仓库模型)和全局配置的企业。多项目工作空间也能为库开发提供支持。
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 application.
如果你打算在工作区中包含多个项目,可以在创建工作空间时不要自动创建初始应用,并为工作空间指定一个唯一的名字。下列命令用于创建一个包含全工作空间级配置文件的工作空间,但没有根应用。
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-appMultiple project file structure
多重项目的文件结构
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
.
工作空间中第一个显式生成的应用会像工作空间中的其它项目一样放在 projects/
文件夹中。新生成的库也会添加到 projects/
下。当你以这种方式创建项目时,工作空间的文件结构与工作空间配置文件 angular.json
中的结构完全一致。
Library project files
库项目文件
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.
当你使用 CLI 命令(例如 ng generate library my-lib
)生成一个库时,所生成的文件会放在工作区的 projects/
文件夹中。关于如何创建自己的库的更多信息,请参阅创建库一章。
Libraries (unlike applications and their associated e2e projects) have their own package.json
configuration files.
与应用及其相关的 e2e 项目不同,库有它自己的 package.json
配置文件。
Under the projects/
folder, the my-lib
folder contains your library code.
在 projects/
目录下,my-lib
文件夹中包含你的库代码。
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/lib |
包含库项目的逻辑和数据。像应用项目一样,库项目也可以包含组件、服务、模块、指令和管道。 |
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/test.ts |
单元测试的主要入口点是一些库专属的配置。你通常不用编辑这个文件。 |
src/public-api.ts |
Specifies all files that are exported from your library. |
src/public-api.ts |
指定从库中导出的所有文件。 |
karma.conf.js |
Library-specific Karma configuration. |
karma.conf.js |
库专属的 Karma 配置。 |
ng-package.json |
Configuration file used by ng-packagr for building your library. |
ng-package.json |
构建库时,ng-packagr 用到的配置文件。 |
package.json |
Configures npm package dependencies that are required for this library. |
package.json |
配置这个库所需的 npm 包依赖。 |
tsconfig.lib.json |
Library-specific TypeScript configuration, including TypeScript and Angular template compiler options. See TypeScript Configuration. |
tsconfig.lib.json |
库专属的 TypeScript 配置,包括 TypeScript 和 Angular 模板编译器选项。参见 TypeScript 配置 。 |
tsconfig.spec.json |
TypeScript configuration for the library tests. See TypeScript Configuration. |
tsconfig.spec.json |
测试库时用到的 TypeScript 配置。参见 TypeScript 配置。 |
tslint.json |
Library-specific TSLint configuration. |
tslint.json |
库专属的 TSLint 配置。 |