Create component to specific module with Angular-CLI
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
Is there a way to create a component to a new module?
e.g.: ng g module newModule
ng g component newComponent (how to add this component to newModule??)
because the angular-cli default behavior is to put all new components inside app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?
add a comment |
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
Is there a way to create a component to a new module?
e.g.: ng g module newModule
ng g component newComponent (how to add this component to newModule??)
because the angular-cli default behavior is to put all new components inside app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?
add a comment |
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
Is there a way to create a component to a new module?
e.g.: ng g module newModule
ng g component newComponent (how to add this component to newModule??)
because the angular-cli default behavior is to put all new components inside app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
Is there a way to create a component to a new module?
e.g.: ng g module newModule
ng g component newComponent (how to add this component to newModule??)
because the angular-cli default behavior is to put all new components inside app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?
edited Jul 11 '18 at 7:35
RajnishCoder
5812727
5812727
asked Nov 17 '16 at 8:20
Elmer DantasElmer Dantas
1,84921725
1,84921725
add a comment |
add a comment |
14 Answers
14
active
oldest
votes
To create a component as part of a module you should
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
You can also stay in the project root and prefix the component with the module nameng g component moduleName/componentName.
– JayChase
Nov 17 '16 at 12:53
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
|
show 4 more comments
ng g component nameComponent --module=app.module.ts
the other answers work, but this is the most efficient. Its worth adding--dry-runto the end of that just to see what will be affected, it's a nice sanity check
– tony09uk
Dec 12 '18 at 21:55
add a comment |
Not sure if maybe Alexander Ciesielski's answer was correct at the time of writing, but I can verify that this no longer works. It doesn't matter which directory in the project you run the Angular CLI. If you type
ng g component newComponent
it will generate a component and import it into the app.module.ts file
The only way you can use CLI to automatically import it into another module is by specifying
ng g component moduleName/newComponent
where moduleName is a module you've already defined in your project. If the moduleName doesn't exist, it'll put the component in moduleName/newComponent directory but still import it into app.module
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.ng g module newModule2.ng g component new-module/newComponentaccording to him should be new-module instead of newModule
– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I justmkdira folder, and then ran n g component newComponent inside of newly created folder.
– Master-Chief
Apr 23 '17 at 3:10
2
I would say that full answer isng g component moduleName/newComponent -m moduleName
– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module withng generate module {modComName}1) Created the folder 2) Created a module file inside the folder of same name ; the commandng generate component {modComName}actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component
– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;ngadds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."
– The Red Pea
May 27 '18 at 14:59
add a comment |
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
ng g module foo
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
ng g component foo/fooList --module=foo.module.ts
And the cli will scaffold out the module and component like this:

--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
ng g component foo/fooList --module=foo
Yay for the--moduleargument; the docs say "Allows specification of the declaring module." ; I would add--modulespecifies which existing module should be modified to import the module you are about to create
– The Red Pea
May 27 '18 at 14:27
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
add a comment |
You can try below command, which describes the,
ng -> Angular
g -> Generate
c -> Component
-m -> Module
Then your command will be like:
ng g c user/userComponent -m user.module
add a comment |
this is what worked for me :
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
If you want to generate a component without its directory use --flat flag.
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
add a comment |
For Angular v4 and Above, simply use:
ng g c componentName -m ModuleName
1
Throws an errorSpecified module does not existwhereas module existed
– Pardeep Jain
May 28 '18 at 10:20
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
add a comment |
Use this simple command:
ng g c users/userlist
users: Your module name.
userlist: Your component name.
add a comment |
According to Angular docs the way to create a component for specific module is,
ng g component <directory name>/<component name>
"directory name" = where the CLI generated the feature module
Example :-
ng generate component customer-dashboard/CustomerDashboard
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
add a comment |
Add a component to the Angular 4 app using Angular CLI
To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under srcapp. Also, the references of the same is added to srcappapp.module.ts file automatically.
A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.
Add a component to specific folder of the Angular 4 app using Angular CLI
To add a new component to a specific folder use the command ng g component folderName/componentName
add a comment |
I am having the similar issues with multiple modules in application. A component can be created to any module so before creating a component we have to specify the name of the particular module.
'ng generate component newCompName --module= specify name of module'
add a comment |
If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
You can :
ng g c my-comp -a app-name
-a stands for --app (name)
add a comment |
First run ng g module newModule
. Then run ng g component newModule/newModule --flat
add a comment |
I am currently working on angular 5 projects and I use this particular command for generating components inside a module.
ng g c <module-name>/<new-component-name>
This command will generate component local to the module if you use normal command eg
ng g c <component-name>
it will generate component inside the app module a.k.a root module.
Note: code enclosed in <> represent user specific names.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40649799%2fcreate-component-to-specific-module-with-angular-cli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
To create a component as part of a module you should
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
You can also stay in the project root and prefix the component with the module nameng g component moduleName/componentName.
– JayChase
Nov 17 '16 at 12:53
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
|
show 4 more comments
To create a component as part of a module you should
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
You can also stay in the project root and prefix the component with the module nameng g component moduleName/componentName.
– JayChase
Nov 17 '16 at 12:53
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
|
show 4 more comments
To create a component as part of a module you should
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
To create a component as part of a module you should
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
edited Dec 12 '16 at 13:48
answered Nov 17 '16 at 8:22
Alexander CiesielskiAlexander Ciesielski
6,12633057
6,12633057
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
You can also stay in the project root and prefix the component with the module nameng g component moduleName/componentName.
– JayChase
Nov 17 '16 at 12:53
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
|
show 4 more comments
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
You can also stay in the project root and prefix the component with the module nameng g component moduleName/componentName.
– JayChase
Nov 17 '16 at 12:53
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
2
2
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
That's exactly what was looking for. Thanks @Alexander. I think they should put this in Docs.
– Elmer Dantas
Nov 17 '16 at 8:34
60
60
You can also stay in the project root and prefix the component with the module name
ng g component moduleName/componentName.– JayChase
Nov 17 '16 at 12:53
You can also stay in the project root and prefix the component with the module name
ng g component moduleName/componentName.– JayChase
Nov 17 '16 at 12:53
1
1
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
With Angular CLI version 1.6.8, I was always getting error sying that 'specified module doesn't exists', when I sepcify an existing module. It worked when I tried the following command: ng generate service service1 --module=module1/module1.module.ts. Note: my service name is service1 and my module name is module1
– Diallo
Feb 11 '18 at 17:58
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
@JayChase's answer is the better option
– shteeven
Mar 13 '18 at 16:07
1
1
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
outdated as of 6th Oct, Angular 6
– tom87416
Oct 6 '18 at 7:57
|
show 4 more comments
ng g component nameComponent --module=app.module.ts
the other answers work, but this is the most efficient. Its worth adding--dry-runto the end of that just to see what will be affected, it's a nice sanity check
– tony09uk
Dec 12 '18 at 21:55
add a comment |
ng g component nameComponent --module=app.module.ts
the other answers work, but this is the most efficient. Its worth adding--dry-runto the end of that just to see what will be affected, it's a nice sanity check
– tony09uk
Dec 12 '18 at 21:55
add a comment |
ng g component nameComponent --module=app.module.ts
ng g component nameComponent --module=app.module.ts
edited Jun 28 '17 at 20:50
JNYRanger
4,64093561
4,64093561
answered Jun 28 '17 at 20:30
Daniel Fernando Acosta CeballoDaniel Fernando Acosta Ceballo
72955
72955
the other answers work, but this is the most efficient. Its worth adding--dry-runto the end of that just to see what will be affected, it's a nice sanity check
– tony09uk
Dec 12 '18 at 21:55
add a comment |
the other answers work, but this is the most efficient. Its worth adding--dry-runto the end of that just to see what will be affected, it's a nice sanity check
– tony09uk
Dec 12 '18 at 21:55
the other answers work, but this is the most efficient. Its worth adding
--dry-run to the end of that just to see what will be affected, it's a nice sanity check– tony09uk
Dec 12 '18 at 21:55
the other answers work, but this is the most efficient. Its worth adding
--dry-run to the end of that just to see what will be affected, it's a nice sanity check– tony09uk
Dec 12 '18 at 21:55
add a comment |
Not sure if maybe Alexander Ciesielski's answer was correct at the time of writing, but I can verify that this no longer works. It doesn't matter which directory in the project you run the Angular CLI. If you type
ng g component newComponent
it will generate a component and import it into the app.module.ts file
The only way you can use CLI to automatically import it into another module is by specifying
ng g component moduleName/newComponent
where moduleName is a module you've already defined in your project. If the moduleName doesn't exist, it'll put the component in moduleName/newComponent directory but still import it into app.module
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.ng g module newModule2.ng g component new-module/newComponentaccording to him should be new-module instead of newModule
– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I justmkdira folder, and then ran n g component newComponent inside of newly created folder.
– Master-Chief
Apr 23 '17 at 3:10
2
I would say that full answer isng g component moduleName/newComponent -m moduleName
– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module withng generate module {modComName}1) Created the folder 2) Created a module file inside the folder of same name ; the commandng generate component {modComName}actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component
– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;ngadds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."
– The Red Pea
May 27 '18 at 14:59
add a comment |
Not sure if maybe Alexander Ciesielski's answer was correct at the time of writing, but I can verify that this no longer works. It doesn't matter which directory in the project you run the Angular CLI. If you type
ng g component newComponent
it will generate a component and import it into the app.module.ts file
The only way you can use CLI to automatically import it into another module is by specifying
ng g component moduleName/newComponent
where moduleName is a module you've already defined in your project. If the moduleName doesn't exist, it'll put the component in moduleName/newComponent directory but still import it into app.module
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.ng g module newModule2.ng g component new-module/newComponentaccording to him should be new-module instead of newModule
– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I justmkdira folder, and then ran n g component newComponent inside of newly created folder.
– Master-Chief
Apr 23 '17 at 3:10
2
I would say that full answer isng g component moduleName/newComponent -m moduleName
– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module withng generate module {modComName}1) Created the folder 2) Created a module file inside the folder of same name ; the commandng generate component {modComName}actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component
– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;ngadds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."
– The Red Pea
May 27 '18 at 14:59
add a comment |
Not sure if maybe Alexander Ciesielski's answer was correct at the time of writing, but I can verify that this no longer works. It doesn't matter which directory in the project you run the Angular CLI. If you type
ng g component newComponent
it will generate a component and import it into the app.module.ts file
The only way you can use CLI to automatically import it into another module is by specifying
ng g component moduleName/newComponent
where moduleName is a module you've already defined in your project. If the moduleName doesn't exist, it'll put the component in moduleName/newComponent directory but still import it into app.module
Not sure if maybe Alexander Ciesielski's answer was correct at the time of writing, but I can verify that this no longer works. It doesn't matter which directory in the project you run the Angular CLI. If you type
ng g component newComponent
it will generate a component and import it into the app.module.ts file
The only way you can use CLI to automatically import it into another module is by specifying
ng g component moduleName/newComponent
where moduleName is a module you've already defined in your project. If the moduleName doesn't exist, it'll put the component in moduleName/newComponent directory but still import it into app.module
answered Jan 31 '17 at 12:16
DiskdriveDiskdrive
6,5772175129
6,5772175129
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.ng g module newModule2.ng g component new-module/newComponentaccording to him should be new-module instead of newModule
– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I justmkdira folder, and then ran n g component newComponent inside of newly created folder.
– Master-Chief
Apr 23 '17 at 3:10
2
I would say that full answer isng g component moduleName/newComponent -m moduleName
– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module withng generate module {modComName}1) Created the folder 2) Created a module file inside the folder of same name ; the commandng generate component {modComName}actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component
– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;ngadds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."
– The Red Pea
May 27 '18 at 14:59
add a comment |
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.ng g module newModule2.ng g component new-module/newComponentaccording to him should be new-module instead of newModule
– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I justmkdira folder, and then ran n g component newComponent inside of newly created folder.
– Master-Chief
Apr 23 '17 at 3:10
2
I would say that full answer isng g component moduleName/newComponent -m moduleName
– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module withng generate module {modComName}1) Created the folder 2) Created a module file inside the folder of same name ; the commandng generate component {modComName}actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component
– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;ngadds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."
– The Red Pea
May 27 '18 at 14:59
2
2
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.
ng g module newModule 2. ng g component new-module/newComponent according to him should be new-module instead of newModule– Elmer Dantas
Jan 31 '17 at 13:23
it was the right answer for me at the time..it works properly. I create a pull request to add this information to the docs. One of it's contributors ask me to remove cd-ing part..in the end will be 1.
ng g module newModule 2. ng g component new-module/newComponent according to him should be new-module instead of newModule– Elmer Dantas
Jan 31 '17 at 13:23
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I just
mkdir a folder, and then ran n g component newComponent inside of newly created folder.– Master-Chief
Apr 23 '17 at 3:10
Just to point out, using technique by Alexander Ciesielski and works as expected. I should note, I did not need to create component simply needed to create folder. So I just
mkdir a folder, and then ran n g component newComponent inside of newly created folder.– Master-Chief
Apr 23 '17 at 3:10
2
2
I would say that full answer is
ng g component moduleName/newComponent -m moduleName– slavugan
Jul 26 '17 at 9:09
I would say that full answer is
ng g component moduleName/newComponent -m moduleName– slavugan
Jul 26 '17 at 9:09
In my case the component name is same as the module name so I created the module with
ng generate module {modComName} 1) Created the folder 2) Created a module file inside the folder of same name ; the command ng generate component {modComName} actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component– The Red Pea
May 27 '18 at 14:36
In my case the component name is same as the module name so I created the module with
ng generate module {modComName} 1) Created the folder 2) Created a module file inside the folder of same name ; the command ng generate component {modComName} actually 1) Created a component file inside the folder of same name 2) Modified the module to import the component– The Red Pea
May 27 '18 at 14:36
Be careful if you create a module and import it into a base module;
ng adds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."– The Red Pea
May 27 '18 at 14:59
Be careful if you create a module and import it into a base module;
ng adds imports to the end of the array; but you may want : "The order of the routes in the configuration matters and this is by design."– The Red Pea
May 27 '18 at 14:59
add a comment |
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
ng g module foo
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
ng g component foo/fooList --module=foo.module.ts
And the cli will scaffold out the module and component like this:

--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
ng g component foo/fooList --module=foo
Yay for the--moduleargument; the docs say "Allows specification of the declaring module." ; I would add--modulespecifies which existing module should be modified to import the module you are about to create
– The Red Pea
May 27 '18 at 14:27
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
add a comment |
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
ng g module foo
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
ng g component foo/fooList --module=foo.module.ts
And the cli will scaffold out the module and component like this:

--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
ng g component foo/fooList --module=foo
Yay for the--moduleargument; the docs say "Allows specification of the declaring module." ; I would add--modulespecifies which existing module should be modified to import the module you are about to create
– The Red Pea
May 27 '18 at 14:27
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
add a comment |
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
ng g module foo
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
ng g component foo/fooList --module=foo.module.ts
And the cli will scaffold out the module and component like this:

--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
ng g component foo/fooList --module=foo
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
ng g module foo
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
ng g component foo/fooList --module=foo.module.ts
And the cli will scaffold out the module and component like this:

--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
ng g component foo/fooList --module=foo
edited Dec 15 '17 at 17:53
answered Aug 4 '17 at 14:19
cobolstinkscobolstinks
2,38393861
2,38393861
Yay for the--moduleargument; the docs say "Allows specification of the declaring module." ; I would add--modulespecifies which existing module should be modified to import the module you are about to create
– The Red Pea
May 27 '18 at 14:27
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
add a comment |
Yay for the--moduleargument; the docs say "Allows specification of the declaring module." ; I would add--modulespecifies which existing module should be modified to import the module you are about to create
– The Red Pea
May 27 '18 at 14:27
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
Yay for the
--module argument; the docs say "Allows specification of the declaring module." ; I would add --module specifies which existing module should be modified to import the module you are about to create– The Red Pea
May 27 '18 at 14:27
Yay for the
--module argument; the docs say "Allows specification of the declaring module." ; I would add --module specifies which existing module should be modified to import the module you are about to create– The Red Pea
May 27 '18 at 14:27
1
1
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
@TheRedPea I believe you meant to say " --module specifies which existing module should be modified to import the component you are ..."
– cobolstinks
Jul 3 '18 at 14:14
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
Yep you're right! Existing module modified with reference to new component
– The Red Pea
Jul 3 '18 at 17:04
add a comment |
You can try below command, which describes the,
ng -> Angular
g -> Generate
c -> Component
-m -> Module
Then your command will be like:
ng g c user/userComponent -m user.module
add a comment |
You can try below command, which describes the,
ng -> Angular
g -> Generate
c -> Component
-m -> Module
Then your command will be like:
ng g c user/userComponent -m user.module
add a comment |
You can try below command, which describes the,
ng -> Angular
g -> Generate
c -> Component
-m -> Module
Then your command will be like:
ng g c user/userComponent -m user.module
You can try below command, which describes the,
ng -> Angular
g -> Generate
c -> Component
-m -> Module
Then your command will be like:
ng g c user/userComponent -m user.module
edited Nov 20 '18 at 12:17
keepAlive
3,16541224
3,16541224
answered Jul 1 '18 at 22:12
user2662006user2662006
1,5351412
1,5351412
add a comment |
add a comment |
this is what worked for me :
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
If you want to generate a component without its directory use --flat flag.
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
add a comment |
this is what worked for me :
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
If you want to generate a component without its directory use --flat flag.
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
add a comment |
this is what worked for me :
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
If you want to generate a component without its directory use --flat flag.
this is what worked for me :
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
If you want to generate a component without its directory use --flat flag.
answered Nov 23 '17 at 16:45
Mohamed MakkaouiMohamed Makkaoui
7828
7828
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
add a comment |
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
don't need to write .ts
– Lapenkov Vladimir
Jun 30 '18 at 7:10
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
--module=path-to-module worked form me thanks @Mohamed Makkaoui
– Ian Poston Framer
Sep 18 '18 at 6:24
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
you're welcome @IanPostonFramer
– Mohamed Makkaoui
Sep 18 '18 at 11:13
add a comment |
For Angular v4 and Above, simply use:
ng g c componentName -m ModuleName
1
Throws an errorSpecified module does not existwhereas module existed
– Pardeep Jain
May 28 '18 at 10:20
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
add a comment |
For Angular v4 and Above, simply use:
ng g c componentName -m ModuleName
1
Throws an errorSpecified module does not existwhereas module existed
– Pardeep Jain
May 28 '18 at 10:20
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
add a comment |
For Angular v4 and Above, simply use:
ng g c componentName -m ModuleName
For Angular v4 and Above, simply use:
ng g c componentName -m ModuleName
edited Apr 4 '18 at 2:41
Stephen Rauch
29.7k153657
29.7k153657
answered Apr 4 '18 at 2:23
ShemzzShemzz
414
414
1
Throws an errorSpecified module does not existwhereas module existed
– Pardeep Jain
May 28 '18 at 10:20
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
add a comment |
1
Throws an errorSpecified module does not existwhereas module existed
– Pardeep Jain
May 28 '18 at 10:20
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
1
1
Throws an error
Specified module does not exist whereas module existed– Pardeep Jain
May 28 '18 at 10:20
Throws an error
Specified module does not exist whereas module existed– Pardeep Jain
May 28 '18 at 10:20
1
1
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
You don't specify the name of the module, you specify the module's filename.
– Roger
Jun 6 '18 at 8:46
add a comment |
Use this simple command:
ng g c users/userlist
users: Your module name.
userlist: Your component name.
add a comment |
Use this simple command:
ng g c users/userlist
users: Your module name.
userlist: Your component name.
add a comment |
Use this simple command:
ng g c users/userlist
users: Your module name.
userlist: Your component name.
Use this simple command:
ng g c users/userlist
users: Your module name.
userlist: Your component name.
edited Jan 29 '18 at 17:32
answered Jan 29 '18 at 13:24
Hasan FathiHasan Fathi
2,19012026
2,19012026
add a comment |
add a comment |
According to Angular docs the way to create a component for specific module is,
ng g component <directory name>/<component name>
"directory name" = where the CLI generated the feature module
Example :-
ng generate component customer-dashboard/CustomerDashboard
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
add a comment |
According to Angular docs the way to create a component for specific module is,
ng g component <directory name>/<component name>
"directory name" = where the CLI generated the feature module
Example :-
ng generate component customer-dashboard/CustomerDashboard
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
add a comment |
According to Angular docs the way to create a component for specific module is,
ng g component <directory name>/<component name>
"directory name" = where the CLI generated the feature module
Example :-
ng generate component customer-dashboard/CustomerDashboard
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
According to Angular docs the way to create a component for specific module is,
ng g component <directory name>/<component name>
"directory name" = where the CLI generated the feature module
Example :-
ng generate component customer-dashboard/CustomerDashboard
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
answered Aug 30 '18 at 11:12
Sksaif UddinSksaif Uddin
17728
17728
add a comment |
add a comment |
Add a component to the Angular 4 app using Angular CLI
To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under srcapp. Also, the references of the same is added to srcappapp.module.ts file automatically.
A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.
Add a component to specific folder of the Angular 4 app using Angular CLI
To add a new component to a specific folder use the command ng g component folderName/componentName
add a comment |
Add a component to the Angular 4 app using Angular CLI
To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under srcapp. Also, the references of the same is added to srcappapp.module.ts file automatically.
A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.
Add a component to specific folder of the Angular 4 app using Angular CLI
To add a new component to a specific folder use the command ng g component folderName/componentName
add a comment |
Add a component to the Angular 4 app using Angular CLI
To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under srcapp. Also, the references of the same is added to srcappapp.module.ts file automatically.
A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.
Add a component to specific folder of the Angular 4 app using Angular CLI
To add a new component to a specific folder use the command ng g component folderName/componentName
Add a component to the Angular 4 app using Angular CLI
To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under srcapp. Also, the references of the same is added to srcappapp.module.ts file automatically.
A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.
Add a component to specific folder of the Angular 4 app using Angular CLI
To add a new component to a specific folder use the command ng g component folderName/componentName
answered Aug 14 '17 at 19:26
studentstudent
11k969130
11k969130
add a comment |
add a comment |
I am having the similar issues with multiple modules in application. A component can be created to any module so before creating a component we have to specify the name of the particular module.
'ng generate component newCompName --module= specify name of module'
add a comment |
I am having the similar issues with multiple modules in application. A component can be created to any module so before creating a component we have to specify the name of the particular module.
'ng generate component newCompName --module= specify name of module'
add a comment |
I am having the similar issues with multiple modules in application. A component can be created to any module so before creating a component we have to specify the name of the particular module.
'ng generate component newCompName --module= specify name of module'
I am having the similar issues with multiple modules in application. A component can be created to any module so before creating a component we have to specify the name of the particular module.
'ng generate component newCompName --module= specify name of module'
answered Sep 9 '17 at 4:32
vivekvivek
546
546
add a comment |
add a comment |
If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
You can :
ng g c my-comp -a app-name
-a stands for --app (name)
add a comment |
If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
You can :
ng g c my-comp -a app-name
-a stands for --app (name)
add a comment |
If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
You can :
ng g c my-comp -a app-name
-a stands for --app (name)
If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
You can :
ng g c my-comp -a app-name
-a stands for --app (name)
answered Apr 24 '18 at 11:20
SamSam
1
1
add a comment |
add a comment |
First run ng g module newModule
. Then run ng g component newModule/newModule --flat
add a comment |
First run ng g module newModule
. Then run ng g component newModule/newModule --flat
add a comment |
First run ng g module newModule
. Then run ng g component newModule/newModule --flat
First run ng g module newModule
. Then run ng g component newModule/newModule --flat
answered Dec 26 '18 at 12:58
Tuan van DuongTuan van Duong
2828
2828
add a comment |
add a comment |
I am currently working on angular 5 projects and I use this particular command for generating components inside a module.
ng g c <module-name>/<new-component-name>
This command will generate component local to the module if you use normal command eg
ng g c <component-name>
it will generate component inside the app module a.k.a root module.
Note: code enclosed in <> represent user specific names.
add a comment |
I am currently working on angular 5 projects and I use this particular command for generating components inside a module.
ng g c <module-name>/<new-component-name>
This command will generate component local to the module if you use normal command eg
ng g c <component-name>
it will generate component inside the app module a.k.a root module.
Note: code enclosed in <> represent user specific names.
add a comment |
I am currently working on angular 5 projects and I use this particular command for generating components inside a module.
ng g c <module-name>/<new-component-name>
This command will generate component local to the module if you use normal command eg
ng g c <component-name>
it will generate component inside the app module a.k.a root module.
Note: code enclosed in <> represent user specific names.
I am currently working on angular 5 projects and I use this particular command for generating components inside a module.
ng g c <module-name>/<new-component-name>
This command will generate component local to the module if you use normal command eg
ng g c <component-name>
it will generate component inside the app module a.k.a root module.
Note: code enclosed in <> represent user specific names.
answered Jan 29 at 4:48
Shashank RawatShashank Rawat
4117
4117
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40649799%2fcreate-component-to-specific-module-with-angular-cli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown