Vince
Vince

Reputation: 1416

How to create component in folder that is not in /src/app with Angular-Cli

I want to be able to create components or modules or services in folder external to /src/app/...

so i've tried with ng g c ../mco/lib but it fail.

I want to be able to create with the CLI a component in folder external to app.

how can i achieve this ?

EDIT

You can change the appRoot of AngularCli, so in angular-cli.json i've added in "apps" array, a property "appRoot": "mco"

Upvotes: 2

Views: 5705

Answers (4)

Kristjan
Kristjan

Reputation: 61

You can just define the right path and the angular-cli will get everything ready for you, for example if you want the component to be build outside app folder on a example folder you can try ng g c ../example/yourComponent

Upvotes: 6

Junliang Huang
Junliang Huang

Reputation: 139

For those who finds appRoot is not working on @angular/cli 6. Here is a workaround:

{
"projects": {
"my-project": {
  "root": "/path/to/project",
  "projectType": "application",
  "schematics": {
      "@schematics/angular:component": {
        "path": "/path/to/project"
      },
      "@schematics/angular:directive": {
        "path": "/path/to/project"
      },
      "@schematics/angular:module": {
        "path": "/path/to/project"
      },
      "@schematics/angular:service":{
        "path": "/path/to/project"
      },
      "@schematics/angular:pipe": {
        "path": "/path/to/project"
      },
      "@schematics/angular:class": {
        "path": "/path/to/project"
      }
  }
}
}

Now you can run

ng g component hello-world --project=my-project --dry-run

to check if the hello-world is output to /path/to/project/hello-world.

Upvotes: 3

Vince
Vince

Reputation: 1416

Change the appRoot of AngularCli.

So in angular-cli.json

  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "tr",
      "appRoot": "mco"
    }
  ],

Upvotes: 2

k13i
k13i

Reputation: 4331

I'm not sure if this feature exists in Angular CLI. Assuming you are in src/app directory you can just create component in default location and then move it to desired place like this:

ng g c my-component; mv my-component /Users/username/desiredlocation/

Upvotes: 0

Related Questions