# Injectable Decorator
Module | import { Injectable } from "@tsed/di" |
---|---|
Source | /packages/di/src/decorators/injectable.ts |
# Overview
function Injectable(options?: Partial<IProvider<any>>): Function;
# Description
The decorators @Injectable()
a new service can be injected in other service, controller, interceptor, etc.. on there constructor
.
All classes annotated with @Injectable()
are built one time, excepted if you change the default provider configuration.
import {Injectable, ProviderScope, ProviderType} from "@tsed/common";
import {Calendar} from "../models/Calendar";
@Injectable({
type: ProviderType.CONTROLLER,
scope: ProviderScope.SINGLETON
})
export class CalendarsService {
private readonly calendars: Calendar[] = [];
create(calendar: Calendar) {
this.calendars.push(calendar);
}
findAll(): Calendar[] {
return this.calendars;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TIP
@Injectable()
use the reflect-metadata
to collect and inject the built provided to other services.
# Options
- type ( or
string
): Kind of provider. (Default:ProviderType.PROVIDER
) - scope (@@ProviderScope@): Kind of provider. (Default:
ProviderScope.SINGLETON
) - deps (
Type<any>
): List of class or provider which will be injected to the constructor (Note: This options override default metadata generated by Typescript).
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing