Angular Architecture - Shared Module
Tags: angular, architecture, frontend, webdev
Published: 2023-04-30 14:24:55

### 🎯 Introduction

Shared Module will contain declarations for reusable components, directives, and pipes, as well as any common services that may be required by multiple feature modules. It may also export third-party modules, like Angular Material or ngx-translate, that are used throughout the application.

To use the Shared Module in other modules, it must be imported into each module that requires access to the shared components, directives, pipes, or services.

![Shared Modules Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5yb8m1mjheoysf0rhtf.png)

```typescript
// Shared Module and export example
@NgModule({
  imports: [CommonModule],
  declarations: [
    MovieCardComponent,
    MovieBackdropColorDirective,
    MovieFilterPipe
  ],
  exports: [
    MovieCardComponent,
    MovieBackdropColorDirective,
    MovieFilterPipe,
    CommonModule,
    FormsModule,
    MatAutocompleteModule,
    MatInputModule,
    MatListModule,
  ],
})
export class SharedModule {}
```

```typescript
// Shared Module import example
@NgModule({
    declarations: [
        ...MovieRoutingModule.components
    ],
    imports: [
        SharedModule,
        MovieRoutingModule,
       
    ],
    providers: [
        MovieResolver
    ]
})
export class MovieModule { }
```

Note: You could as well create your shared components as **standalone** components or export each component as it's own module.
But I believe that if you have a large number of shared components or your application is more complex, exporting shared components through a SharedModule is generally preferred as it's much easier to mantain. 

![hr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2xe9kuogtn0ee8zcobn.png)

### 🎯 The Benefits 

It's usually a common requirement of many projects to have customized building blocks for the applications such as usual UI elements like buttons, dropdowns, alerts, cards, tables, checkboxes, text inputs, file inputs, select elements and so on.

One of the key benefits of using a SharedModule is that it allows you to consolidate your presentational dummy components, which can be treated as a 3rd party library project. This makes it easier to manage and reuse your components across multiple projects (if needed), as well as share them with other developers or teams.

You can easily update and test your components in isolation, and make changes without affecting the functionality of your main application.

So it's a great place to start writing your unit tests and use something like [Storybook](https://storybook.js.org/) stories.

_Although it can be tempting to skip unit testing, starting with the smaller parts like testing your presentational components can help you get in the habit and reap the benefits of more robust and reliable code._

Storybook is a powerful tool that enables you to:
- Develop and test your components independently of your main application
- Define multiple use cases for your components, providing a clearer overview of the various scenarios in which they can be utilize
- Document your components thoroughly
- Enhance communication between team members by providing a centralized location for component viewing and collaboration.

![mongodbstorybook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfgjzfcw0s5mmxvfmn9r.png)

![hr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2xe9kuogtn0ee8zcobn.png)