2019-12-02 15:47:19 -05:00
# Sharing modules
2017-07-04 10:58:20 -04:00
Creating shared modules allows you to organize and streamline your code. You can put commonly
used directives, pipes, and components into one module and then import just that module wherever
you need it in other parts of your app.
Consider the following module from an imaginary app:
```typescript
2018-01-17 07:57:43 -05:00
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CustomerComponent } from './customer.component';
import { NewItemDirective } from './new-item.directive';
import { OrdersPipe } from './orders.pipe';
2017-07-04 10:58:20 -04:00
@NgModule ({
imports: [ CommonModule ],
2018-01-17 07:57:43 -05:00
declarations: [ CustomerComponent, NewItemDirective, OrdersPipe ],
exports: [ CustomerComponent, NewItemDirective, OrdersPipe,
2017-07-04 10:58:20 -04:00
CommonModule, FormsModule ]
})
export class SharedModule { }
```
Note the following:
* It imports the `CommonModule` because the module's component needs common directives.
* It declares and exports the utility pipe, directive, and component classes.
* It re-exports the `CommonModule` and `FormsModule` .
By re-exporting `CommonModule` and `FormsModule` , any other module that imports this
`SharedModule` , gets access to directives like `NgIf` and `NgFor` from `CommonModule`
and can bind to component properties with `[(ngModel)]` , a directive in the `FormsModule` .
Even though the components declared by `SharedModule` might not bind
with `[(ngModel)]` and there may be no need for `SharedModule`
to import `FormsModule` , `SharedModule` can still export
`FormsModule` without listing it among its `imports` . This
way, you can give other modules access to `FormsModule` without
having to import it directly into the `@NgModule` decorator.
2019-06-20 14:05:25 -04:00
### Using components vs services from other modules
2017-07-04 10:58:20 -04:00
There is an important distinction between using another module's component and
2018-01-11 15:38:51 -05:00
using a service from another module. Import modules when you want to use
2018-01-17 07:57:43 -05:00
directives, pipes, and components. Importing a module with services means that you will have a new instance of that service, which typically is not what you need (typically one wants to reuse an existing service). Use module imports to control service instantiation.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
The most common way to get a hold of shared services is through Angular
2017-07-04 10:58:20 -04:00
[dependency injection ](guide/dependency-injection ), rather than through the module system (importing a module will result in a new service instance, which is not a typical usage).
To read about sharing services, see [Providers ](guide/providers ).
< hr / >
## More on NgModules
You may also be interested in the following:
* [Providers ](guide/providers ).
2018-01-17 07:57:43 -05:00
* [Types of Feature Modules ](guide/module-types ).