生成的应用程序 <app-name> 和新应用程序 <app-new> 之间的差异

difference between ng generate application <app-name> and ng new <app-new>

我知道 ng generate application <app-name> 仅在 angular 应用程序文件夹中有效的区别。但是其他的区别是什么。

ng new NAME 使用默认应用程序创建新工作区

ng g application NAME 在现有工作区中创建新应用程序

官方文档没有详细解释这一点。因此,我建议挖掘一些博客 post 来介绍它 post。比如下面的:

Angular 6 Workspace

ng init:在当前目录新建一个应用程序

ng new: 创建一个新目录并在新目录中 运行 ng init.

ng generate: 向现有应用程序添加功能,例如添加模块, 组件和服务等生成命令和不同的子 命令也有快捷符号,所以 ng g 命令是 类似于 ng 生成

长话短说:

  • ng new <app-name> 创建一个新工作区 (默认情况下)在该工作区中创建一个新应用程序。
  • ng generate application <app-name> 现有 工作区中创建一个新应用程序。

详细信息

参考了多个来源,以备您想阅读更多内容。但这里有一个很好的概述。

概览

Workspace - Angular 项目(即应用程序和库)的集合,由 Angular CLI 提供支持,通常位于单个源代码控制存储库(例如 git)。 资料来源:https://angular.io/guide/glossary#workspace

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.

The Angular CLI ng new command creates a workspace.

ng new <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.

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.

来源:https://angular.io/guide/file-structure#workspace-and-project-file-structure

When you generate an additional app or library in a workspace, it goes into a projects/ subfolder.

A newly generated app contains the source files for a root module, with a root component and template. Each app has a src folder that contains the logic, data, and assets.

来源:https://angular.io/cli#workspaces-and-project-files

示例

没有应用程序的工作区

正在使用 --createApplication=false 标志创建新项目,因此不会在工作区内创建任何应用程序(默认为 true)。请注意,我们的工作区中没有 projectssrc 文件夹。

> ng new my-workspace --createApplication=false

现在进入创建的工作区文件夹并生成一个应用程序,您将看到我们的工作区中现在有一个 projects/my-app 文件夹。

> cd my-workspace
> ng generate app my-app

您还可以再次使用 ng generate 在工作区中创建第二个应用程序,您将在我们的工作区中看到这两个应用程序。

>ng generate app my-other-app

带应用程序的工作区

较小项目的另一种更常见的方法是使用 ng new 不使用 createApplication 标志(或通过将其设置为 true)。在这里您将获得一个工作区,其中 src 文件夹包含我们的应用程序。

>ng new my-app
或者
>ng new my-app --createApplication=true