Bootstrapping Providers
As of Angular 6, we have access to a great way to have our services globally available in our applications, while still being bundled and lazy loaded as efficiently as possible. @Provider decorators now take an additional argument, providedIn.
In almost every case, you want to use providedIn: 'root' - telling your angular application that this service should be a singleton, available all throughout your application. You don't even need to inject it into a module!
app/greeter.service.ts
1
import { Injectable } from "@angular/core";
2
3
@Injectable({
4
providedIn: "root"
5
})
6
export class GreeterService {
7
private message: string =
8
"Registering Providers while Bootstrapping an Angular 2 application!";
9
10
getMessage() {
11
return this.message;
12
}
13
}
Copied!
Last modified 3mo ago
Copy link