Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Cannot use directive form a nested module

I am trying to define a directive that would hide elements if a user doesn't have a certain claim.

After i define the directive i declare it in application.module declarations.

The problem is that i am unable to use the directive from a module that is nested inside of application.module. If i use the directive inside a component that is 'next' to application module it works fine, but when i use the directive from a component that is inside of a nested module then it is not recognized by compiler and throws an error and if i declare the directive again compiler throws up again because it is being declared elsewhere.

like image 524
Vilmantas Petrusevicius Avatar asked Jan 20 '26 18:01

Vilmantas Petrusevicius


1 Answers

Declarations are not inherited by nested modules .You need to add your Directive into declarations for each NgModule where you want to use it. Also you can have a wrapper module for your directive like YourDirectiveModule, add your Directive into the declarations and then export them from YourDirectiveModule. Then import your YourDirectiveModule into each NgModule where you want to use it.

@NgModule({
   declarations: [YourDirective],
   exports: [YourDirective]
})
export class YourDirectiveModule { }

And use

@NgModule({
   imports: [ YourDirectiveModule ]
})
export class SomeModule
like image 139
Suren Srapyan Avatar answered Jan 22 '26 12:01

Suren Srapyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!