HI WELCOME TO SIRIS

angular cli generate directives, pipes and routing guards

Leave a Comment

In this video we will discuss generating directives, pipes and routing guards using the Angular CLI.


Generating directives, pipes, routing guards and other angular features is very similar to generating component and services. We discussed generating components and services using the Angular CLI in our previous videos in this course.

Angular FeatureComplete CommandAlias
Directiveng generate directive directiveNameng g d directiveName
Pipeng generate pipe pipeNameng g p pipeName
Routing Guardng generate guard guardNameng g g guardName

Please note : When you try to generate a directive, pipe or a component, and if you have multiple modules in your angular project you may get the following error
More than one module matches. Use skip-import option to skip importing the component into the closest module.

The reason we are getting this error is we have more than one module in our angular project, so Angular CLI does not know with which module the newly generated directive, pipe or component should be registered. So we have 2 options here.
  1. Use --skip-import option to tell Angular CLI not to import and register the generated component, directive or pipe 
    ng g d directiveName --skip-import -d
  2. Use --module option or it's alias -m to tell Angular CLI the module with which we want our newly generated component, directive or pipe should be registered. 
    ng g d directiveName -m=app.module -d
If you have just one module in your Angular project, then you wouldn't get this error, as the angular cli will automatically import and register the newly generated component, directive or pipe with that one existing module.

When generating certain angular features like services or routing guards, you will not get this error, even when you have multiple modules in your project, because by default, Angular CLI does not try to import and register these features.

Please note that we can always use the following options along with ng generate command to customize the generation of directives, pipes and routing guards using the Angular CLI.

OptionPurpose
flatSpecifies if a dedicated folder should be created
moduleSpecifies the module with which the newly generated angular feature should be registered
specSpecifies if a spec file should be generated

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.