I can't add new component with angular cli in

2020-01-30 09:06发布

I want to add a new component in asp.net core 2.0 SPAs with angular 4 with angular cli with this version:

@angular/cli: 1.4.2

node: 8.4.0

os: Linux x64

when I running this command ng g c vehicle-form on a directory of components got this error:

Error: Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule. Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule.

app.module.shared.ts

Also, I have 3 files for app.module.ts not one :\ :

app.module.browser.ts

app.module.shared.ts

app.module.server.ts

8条回答
我命由我不由天
2楼-- · 2020-01-30 09:13

I ran into the same issue and then I realized I was not in to root folder of the project.

I did not work when

/c/some/path/your_project/src/bla/bla/bla

But it does work (for me it worked)

/c/some/path/your_project
查看更多
贪生不怕死
3楼-- · 2020-01-30 09:14

I had the same problem while converting 5 to 6 version. I found the solution from github. when I removed e2e from sourceRoot It solved the problem.

查看更多
4楼-- · 2020-01-30 09:15

ng generate component componentName --module=app.module

In my project also I got the above error I tried this command this works properly.I think this will help you.

查看更多
Rolldiameter
5楼-- · 2020-01-30 09:25

In the case of an ASP Net Core Angular project, the app.module is split across 3 files as you've highlighted. The angular CLI has to decide where to add the reference to the component in the module, so you need to provide it with some help.

ng g c moduleName --module='app.module.browser.ts'

查看更多
虎瘦雄心在
6楼-- · 2020-01-30 09:30

You're using the Angular CLI in a project that wasn't built with the CLI in mind, so you have to take a couple of extra steps. The error message tells you want to do.

First, run the ng g c vehicle-form command and include the --skip-import=true option.

Then, add the component declaration in the proper application module file by hand. In this case, the app.module.shared.ts, where all the other application components are declared.

Or, you can run the ng g c vehicle-form and include the --module='app.module.shared.ts'. You may have to fully qualify the path to the module file with this. I haven't tested it, so be prepared to try a few things with this on your own.

More on ng generate component: generate component.

UPDATE (2018-03-01)

Microsoft has delivered some new SPA templates for use with ASP.NET Core 2.0 applications. You can find more details about them here. The new Angular template includes support for the Angular CLI. So, if you are familiar with the CLI, then this should feel a little bit more like home. This means you can to the ng g commands to generate code with this new template.

查看更多
手持菜刀,她持情操
7楼-- · 2020-01-30 09:35

This error will occur in case if app.module.ts file is not present. Please verify if the file is present and if it does not exist, create one manually.

查看更多
登录 后发表回答