Skip to content

service structure

Raphaël Balet edited this page Dec 6, 2023 · 6 revisions

Where to import which service

Global

This service will be injected only once, could be into the app.module.ts
Example: AppThemeService, SessionService, DarkModeService, ...

@Injectable({
  providedIn: 'root',
})
export class sessionService {/** ... **/}

Folder structure

src
└── app
    └── core
        └── services
            ├── [name].service.ts
            ├── session.service.ts // example
            └── dal.service.ts // example

Shared

Meant to be imported multiple time across the project
Example: loaderService, qrCodeService, ...

@Injectable()
export class loaderService {/** ... **/}

Folder structure

src
└── app
    └── shared
        └── services
            └── loader.service.ts  // <-- Here

Local

This service will be injected into the component directly

src
└── app
    └── pages
        └── [page]
            ├── [page].component.html
            ├── [page].component.ts
            ├── [page].module.ts
            └── [page].service.ts 
// [page].module.ts
@NgModule({
  declarations: [],
  imports: [],
  providers: [pageService], // <-- Here
})

or

// [page].component.ts
@Component({
  standalone: true,
  declarations: [],
  imports: [],
  providers: [pageService], // <-- Here
})

Clone this wiki locally