Use AngularCLI to generate components when there are multiple apps in your project

人走茶凉 提交于 2019-12-24 03:34:25

问题


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

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?


回答1:


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 --> creates a component in the first app

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



来源:https://stackoverflow.com/questions/48759741/use-angularcli-to-generate-components-when-there-are-multiple-apps-in-your-proje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!