Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

angular cli generate directives, pipes and routing guards

Suggested Videos
Part 9 - Angular cli generate component | Text | Slides
Part 10 - Angular cli generate service | Text | Slides
Part 11 - Angular cli generate module | Text | Slides

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 Feature Complete Command Alias
Directive ng generate directive directiveName ng g d directiveName
Pipe ng generate pipe pipeName ng g p pipeName
Routing Guard ng generate guard guardName 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.

Option Purpose
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

angular cli tutorial for beginners

No comments:

Post a Comment

It would be great if you can help share these free resources