问题
I have the following directory structure
I would like to create a new page, let's say, an About page. I want to put it in src/app/page/about/*
So I try:
ng generate component pages/about
but I get this error:
Error: More than one module matches. Use skip-import option to skip importing the component into the closest module.
More than one module matches. Use skip-import option to skip importing the component into the closest module.
Is it a good idea to use pages to store my separate pages? How can I generate components in a subdirectory using the angular-cli?
回答1:
Because there are 2 modules (app.module.ts
, shared.module.ts
) found the CLI does not know which module to declare your component in. To get around this you have to tell the CLI which module you wish to declare your component in with the module
option
ng generate component pages/about --module=app.module
// or
ng generate component pages/about --module=shared.module
回答2:
To create a component as part of any new module you could also do :
cd newModule to change directory into the newModule folder
ng g c newComponent to create a component as a child of the module.
This worked out for me. I was getting same error as you received.
回答3:
In general need to generate component in particular path or not more than one parent module like above case.
$> ng g component path/component-name
No need of src/app/path/component-name ->path .if it is AgnularCLI app.
if you mention src/app "Error: More than one module matches. Use skip-import "occur.
来源:https://stackoverflow.com/questions/48102863/ng-generate-component-in-subdirectory