Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Angular Project Structure: Shared Module
Simi Lika
Simi Lika

Posted on

     

Angular Project Structure: Shared Module

This post provides an overview of theSharedModule in Angular and guides you on its integration into your project structure. TheSharedModule is designed to hold reusable components, pipes, and other utilities that can be shared across multiple modules in your application.

The module might contain:

-Components: Reusable UI components such as headers, footers, buttons, and dialogs.
-Pipes: Custom pipes for formatting and transforming data.
-Directives: Custom directives for reusable behaviors.
-Services: Utilities and helper services that can be used throughout the project.

Shared Module Structure

Here's an example structure for the Shared Module:

/my-angular-project|-- src|   |-- app|   |   |-- shared|   |   |   |-- shared.module.ts|   |   |   |-- components|   |   |   |   |-- header|   |   |   |   |   |-- header.component.ts|   |   |   |   |   |-- header.component.html|   |   |   |   |-- footer|   |   |   |       |-- footer.component.ts|   |   |   |       |-- footer.component.html|   |   |   |-- pipes|   |   |       |-- capitalize.pipe.ts|   |   |
Enter fullscreen modeExit fullscreen mode

Shared Module Implementation

1. Creating the Shared Module

Create a shared.module.ts file inside the shared directory.

// src/app/shared/shared.module.tsimport{NgModule}from'@angular/core';import{CommonModule}from'@angular/common';import{HeaderComponent}from'./components/header/header.component';import{FooterComponent}from'./components/footer/footer.component';import{CapitalizePipe}from'./pipes/capitalize.pipe';@NgModule({declarations:[HeaderComponent,FooterComponent,CapitalizePipe],imports:[CommonModule],exports:[HeaderComponent,FooterComponent,CapitalizePipe]})exportclassSharedModule{}
Enter fullscreen modeExit fullscreen mode

2. Creating Components

  • Header Component
// src/app/shared/components/header/header.component.tsimport{Component}from'@angular/core';@Component({selector:'app-header',templateUrl:'./header.component.html'})exportclassHeaderComponent{}
Enter fullscreen modeExit fullscreen mode
<!-- src/app/shared/components/header/header.component.html --><header><h1>Header Component</h1></header>
Enter fullscreen modeExit fullscreen mode
  • Footer Component
// src/app/shared/components/footer/footer.component.tsimport{Component}from'@angular/core';@Component({selector:'app-footer',templateUrl:'./footer.component.html'})exportclassFooterComponent{}
Enter fullscreen modeExit fullscreen mode
<!-- src/app/shared/components/footer/footer.component.html --><footer><p>Footer Component</p></footer>
Enter fullscreen modeExit fullscreen mode

3. Creating a Pipe

// src/app/shared/pipes/capitalize.pipe.tsimport{Pipe,PipeTransform}from'@angular/core';@Pipe({name:'capitalize'})exportclassCapitalizePipeimplementsPipeTransform{transform(value:string):string{returnvalue.charAt(0).toUpperCase()+value.slice(1);}}
Enter fullscreen modeExit fullscreen mode

4. Export Everything for Easier Imports

  • Index for Components
// src/app/shared/components/index.tsexport*from'./header/header.component';export*from'./footer/footer.component';
Enter fullscreen modeExit fullscreen mode
  • Index for Pipes
// src/app/shared/pipes/index.tsexport*from'./capitalize.pipe';
Enter fullscreen modeExit fullscreen mode

5. Using the Shared Module

Finally, import theSharedModule into other modules where you want to use the shared components and pipes.

// src/app/feature/feature.module.tsimport{NgModule}from'@angular/core';import{CommonModule}from'@angular/common';import{SharedModule}from'../shared/shared.module';import{FeatureComponent}from'./feature.component';@NgModule({declarations:[FeatureComponent],imports:[CommonModule,SharedModule]})exportclassFeatureModule{}
Enter fullscreen modeExit fullscreen mode
// src/app/app.module.tsimport{BrowserModule}from'@angular/platform-browser';import{NgModule}from'@angular/core';import{AppComponent}from'./app.component';import{SharedModule}from'./shared/shared.module';import{FeatureModule}from'./feature/feature.module';@NgModule({declarations:[AppComponent],imports:[BrowserModule,SharedModule,FeatureModule],providers:[],bootstrap:[AppComponent]})exportclassAppModule{}
Enter fullscreen modeExit fullscreen mode

This is a short intro to theSharedModule for your Angular project. In the next post, we will talk about creating and structuringFeaturesModule. Happy coding!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey there! 👋 I'm a passionate full-stack developer with Angular, Ionic, Python, and PostgreSQL expertise. I love building sleek and efficient web and mobile applications that bring ideas to life.
  • Joined

More fromSimi Lika

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp