Blog>
Snippets

Sharing Data with NgModules via a Shared Module

Create a shared module using Angular's NgModule to share common components, directives, and pipes between various feature modules in the application.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedComponent } from './shared.component';

@NgModule({
  imports: [CommonModule],
  declarations: [SharedComponent],
  exports: [SharedComponent]
})
export class SharedModule { }
Defines a SharedModule. This module imports CommonModule from Angular's core, declares SharedComponent, and exports SharedComponent so that other modules can use it when they import SharedModule.
import { SharedModule } from './shared/shared.module';

@NgModule({
  imports: [
    SharedModule,
    // ...other modules this feature module needs
  ],
  //...other metadata like declarations, providers etc.
})
export class FeatureModule { }
Imports the SharedModule into a feature module called FeatureModule. This allows FeatureModule to use components, directives, and pipes declared in SharedModule.