Angular cli generate directives, pipes and routing guards04:33

  • 0
Published on November 19, 2017

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.

To generate a directive use
ng generate directive directiveName OR ng g d directiveName

To generate a pipe use
ng generate pipe pipeName OR ng g p pipeName

To generate a routing guard use
ng generate guard guardName OR ng 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 genearting 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 customise the generation of directives, pipes and routing guards using the Angular CLI.

flat – Specifies if a dedicated folder should be created
module – Specifies the module with which the newly generated angular feature should be rigstered
spec – Specifies if a spec file should be generated

Text version of the video

Slides

Angular CLI Tutorial

Angular CLI Text articles & Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

Enjoyed this video?
"No Thanks. Please Close This Box!"