当您的项目中有多个应用程序时,使用 AngularCLI 生成组件 [英] Use AngularCLI to generate components when there are multiple apps in your project

查看:20
本文介绍了当您的项目中有多个应用程序时,使用 AngularCLI 生成组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 .angular-cli.json 文件如下所示:

My .angular-cli.json file looks like this:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "suman-chrome-extension"
  },
  "apps": [

    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
    },
    {
      "root":"blank-page-src",
      "outDir": "blank-page-dist",
      "index": "page.html",
      "main": "page.ts",
      "polyfills": "polyfills.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "blank-page",
    },

    {
      "root":"dev-tools-src",
      "outDir": "dev-tools-dist",
      "index": "suman-dev-tools.html",
      "main": "suman-dev-tools.ts",
      "polyfills": "polyfills.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "suman-dev-tools",
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {
    }
  }
}

我遇到的问题是,当我使用这样的命令时:

The problem I am having is that when I use a command like this:

ng g service services/foo

它将把这个服务放在第一个应用程序中.即使我 cd 进入子目录,也会发生这种情况.

it will put this service in the first app. This occurs even if I cd into a subdirectory.

如何在主应用程序/第一个应用程序以外的应用程序中生成组件/服务等?

How can I generate components/services etc in an app other than the main/first one?

推荐答案

你应该给他们每个人起一个像下面这样的名字

You should give each of them a name like following

.angular-cli.json

apps: [
    {
        ...
        "name": "app-1"
        ...
    },
    {
        ...
        "name": "app-2"
        ...
    }
]

然后,你可以像下面这样创建你的组件/服务/管道/模块

Then, you can create your components/services/pipes/modules like following

ng g c my-component --app=app-1

或者如果你不想给它们命名(你应该命名),你也可以使用索引,如下所示

Or if you don't want to name them (which you should), you can use index as well like following

ng g c my-component --app=0 --> 在第一个应用程序中创建一个组件

ng g c my-component --app=0 --> creates a component in the first app

ng g c my-component --app=1 --> 在第二个应用程序中创建一个组件

ng g c my-component --app=1 --> creates a component in the second app

这篇关于当您的项目中有多个应用程序时,使用 AngularCLI 生成组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆