Angular's DI
The last example introduced a hypothetical Injector
object. Angularsimplifies DI even further. With Angular, programmers almost never have to getbogged down with injection details.
Angular's DI system is (mostly) controlled through @NgModule
. Specificallythe providers
and declarations
array. (declarations
is where we put components,pipes and directives; providers
is where we put services)
For example:
import { Injectable, NgModule } from '@angular/core';
@Component({
// ...
})
class ChatWidget {
constructor(private authService: AuthService, private authWidget: AuthWidget,
private chatSocket: ChatSocket) {}
}
@NgModule({
declarations: [ ChatWidget ]
})
export class AppModule {
};
In the above example the AppModule
is told about the ChatWidget
class. Another wayof saying this is that Angular has been provided a ChatWidget
.
That seems pretty straightforward, but astute readers will be wondering howAngular knows how to build ChatWidget
. What if ChatWidget
was a string, ora plain function?
Angular assumes that it's being given a class.
What about AuthService
, AuthWidget
and ChatSocket
? How is ChatWidget
getting those?
It's not, at least not yet. Angular does not know about them yet. That canbe changed easily enough:
import { Injectable, NgModule } from '@angular/core';
@Component({
// ...
})
class ChatWidget {
constructor(private authService: AuthService, private authWidget: AuthWidget,
private chatSocket: ChatSocket) {}
}
@Component({
// ...
})
class AuthWidget {}
@Injectable()
class AuthService {}
@Injectable()
class ChatSocket {}
@NgModule({
declarations[ ChatWidget, AuthWidget ]
providers: [ AuthService, ChatSocket ],
})
Okay, this is starting to look a little bit more complete. Although it's stillunclear how ChatWidget
is being told about its dependencies. Perhaps that isrelated to those odd @Injectable
statements.
原文: https://angular-2-training-book.rangle.io/handout/di/angular2/