If I have three modules
Inside the SharedModule I have a PaginatorComponent that is exported as follows:
shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PaginatorComponent } from './paginator/paginator.component';
@NgModule({
imports: [
CommonModule
],
declarations: [
PaginatorComponent
],
providers: [],
exports: [
PaginatorComponent
]
})
export class SharedModule { }
I then import the shared module into my AppModule using the lines
app.module.ts
import { SharedModule } from './shared/shared.module';
imports: [
...
SharedModule,
...
Question; Should my third "ItemModule" automatically have access to the PaginatorComponent or do I need to import directly into the 3rd module? Currently I get a variation of the generic error:
Can't bind to 'totalItems' since it isn't a known property of 'app-paginator'.
1. If 'app-paginator' is an Angular component and it has 'totalItems' input, then verify that it is part of this module.
Many thanks.
providers of a bootstrapped module have application scope. Adding a service provider to @NgModule. providers effectively publishes the service to the entire application. When you import an NgModule, Angular adds the module's service providers (the contents of its providers list) to the application root injector.
It serves as a registry (aka container) for all the components, pipes, directives and providers in your application. An import is what you put in the imports property of the @NgModule decorator. It enables an Angular module to use functionality that was defined in another Angular module.
Services are one of fundamental blocks of every Angular application. Service is just a TypeScript class with or even without @Injectable decorator. When you provide the service at the root level, Angular creates a single, shared instance of service and injects into any class that asks for it.
Importing moduleslink When you use these Angular modules, import them in AppModule , or your feature module as appropriate, and list them in the @NgModule imports array. For example, in the basic application generated by the Angular CLI, BrowserModule is the first import at the top of the AppModule , app. module. ts .
This is something I misunderstood when I started Angular 2+/
You need to re-import every component you use in every module.
With you example, if you need to use PaginatorComponent
into your ItemModule
, you will need to import SharedModule
into your ItemModule
to make it work. (because SharedModule exports PaginatorComponent)
This is not the case for services/providers. If you inject a service, every child component will be able to access it.
The answer can be found at https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407 thanks to @Rama
In short, the answer is
If the module is imported for components, you’ll need to import it in each module needing them;
If the module is imported for services, you’ll need to import it only once, in the first app module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With