28
loading...
This website collects cookies to deliver better user experience
@Injectable()
@Injectable()
class MyService {}
@Component()
class MyComponent {
constructor(private service: MyService) {}
}
Null Injector
which is saying that service is not provided any where so it is not usable!NgRx
as state management as it is too complicate and we are too lazy to learn it!@Injectable({ providedIn: 'root' })
class MySingletonService {}
providedIn
field, which will tell Angular to provide it in root of our app, so we will have one instance of our service in the app.@Injectable()
class MyService {}
@NgModule({
providers: [MyService]
})
class FirstModule{}
@NgModule({
providers: [MyService]
})
class SecondModule{}
@NgModule({
providers: [MyService]
})
class ThirdModule{}
providers
field.@Injectable()
class MySrvice {}
@Component({
providers: [MyService]
})
class MyComponent {
constructor(private service: MyService) {}
}
@Injectable()
class MySrvice implements OnDestroy {}
OnDestroy
interface/lifecycle on service this way!