Welcome to the Shared Modules lesson in Angular! In this tutorial, we'll explore how to create and use shared modules in your Angular applications. Shared modules are essential for organizing your application, improving reusability, and promoting better code management. š” Pro Tip: Shared modules can be thought of as a toolbox, where we store reusable parts of our application.
Shared modules are Angular modules that contain components, directives, and services that can be used across different feature modules. They help in reducing duplication and promoting code reusability.
š Note: In Angular, a module is a collection of related components, directives, pipes, and services.
Let's create a simple shared module called SharedModule.
ng generate module sharedThis command generates a new Angular module named SharedModule.
To use the shared module, we need to import it into our feature module. For example, if we have a HomeModule and we want to use a component from SharedModule, we'd do:
import { SharedModule } from './shared/shared.module';
@NgModule({
imports: [
SharedModule
]
})
export class HomeModule { }To declare a component for sharing, we'll add it to the declarations array of the shared module.
// src/shared/shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MySharedComponent } from './my-shared.component';
@NgModule({
declarations: [MySharedComponent],
imports: [CommonModule],
exports: [MySharedComponent]
})
export class SharedModule { }š Note: The exports array contains the components, directives, and services that can be used outside of the shared module.
Now that we've created a shared component, we can use it in our feature module.
<!-- src/app/home/home.component.html -->
<app-my-shared></app-my-shared>Let's create a shared service for managing authentication.
ng generate service authIn auth.service.ts, we'll define methods for user login and logout.
// src/shared/auth.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AuthService {
login(username: string, password: string) {
// Implement user login logic here
}
logout() {
// Implement user logout logic here
}
}Now, let's add the AuthService to our shared module.
// src/shared/shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MySharedComponent } from './my-shared.component';
import { AuthService } from './auth.service';
@NgModule({
declarations: [MySharedComponent],
imports: [CommonModule],
providers: [AuthService],
exports: [MySharedComponent, AuthService]
})
export class SharedModule { }We've added the AuthService to the providers array, making it available throughout the application. Now, we can inject the AuthService in any component or service that requires user authentication.
What does a shared module contain?
With this, you've learned the basics of creating and using shared modules in Angular. As you continue working with Angular, you'll find shared modules to be a valuable asset in organizing your application and promoting code reusability. š” Pro Tip: Always remember, the goal is to keep your code organized, maintainable, and reusable. Happy coding! š