经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » AngularJS2 » 查看文章
详解Angular依赖注入
来源:jb51  时间:2021/5/24 12:12:42  对本文有异议

概述

依赖注入:设计模式

依赖:程序里需要的某种类型的对象。

依赖注入框架:工程化的框架

注入器Injector:用它的API创建依赖的实例

Provider:怎样创建?(构造函数,工程函数)

Object:组件,模块需要的依赖

依赖性注入进阶=>Angular中依赖注入框架提供父子层次注入型依赖

一、依赖注入

  1. class Id {
  2. static getInstance(type: string): Id {
  3. return new Id();
  4. }
  5. }
  6.  
  7. class Address {
  8. constructor(provice, city, district, street) {}
  9. }
  10.  
  11. class Person {
  12. id: Id;
  13. address: Address;
  14. constructor() {
  15. this.id = Id.getInstance("idcard");
  16. this.address = new Address("北京", "背景", "朝阳区", "xx街道");
  17. }
  18. }

问题:Person需要清楚的知道Address和Id的实现细节。

ID和Address重构后,Person需要知道怎么重构。

项目规模扩大后,集成容易出问题。

  1. class Id {
  2. static getInstance(type: string): Id {
  3. return new Id();
  4. }
  5. }
  6.  
  7. class Address {
  8. constructor(provice, city, district, street) {}
  9. }
  10.  
  11. class Person {
  12. id: Id;
  13. address: Address;
  14. constructor(id: Id, address: Address) {
  15. this.id = id;
  16. this.address = address;
  17. }
  18. }
  19.  
  20. main(){
  21. //把构造依赖对象,推到上一级,推调用的地方
  22. const id = Id.getInstance("idcard");
  23. const address = new Address("北京", "背景", "朝阳区", "xx街道");
  24. const person = new Person(id , address);
  25. }

Person已经不知道Id和Address的细节了。

这是最简单的依赖注入。

问题是在main里还是需要知道细节。

思路:一级一级往上推,一直推到入口函数,入口函数来处理所有对象的构造。构造出来后提供给所有依赖的子模块的子类。

问题:入口函数很难维护。所以需要一个依赖注入框架帮助完成。

二、Angular的依赖注入框架

从v5开始,因为速度慢,引入大量代码已弃用,改为Injector.create。

ReflectiveInjector :用于实例化对象和解析依赖关系。import { Component ,ReflectiveInjector } from "@angular/core";resolveAndCreate接收一个provider数组,provider告诉injector应该怎样去构造这个对象。

  1. constructor() {
  2. //接收一个provider数组
  3. const injector = ReflectiveInjector.resolveAndCreate([
  4. {
  5. provide: Person, useClass:Person
  6. },
  7. {
  8. provide: Address, useFactory: ()=>{
  9. if(environment.production){
  10. return new Address("北京", "背景", "朝阳区", "xx街道xx号");
  11. }else{
  12. return new Address("西藏", "拉萨", "xx区", "xx街道xx号");
  13. }
  14. }
  15. },
  16. {
  17. provide: Id, useFactory:()=>{
  18. return Id.getInstance('idcard');
  19. }
  20. }
  21. ]);
  22. }

Injector:

injector相当于main函数,可以拿到所有依赖池子里的东西。

  1. import { Component ,ReflectiveInjector, Inject} from "@angular/core";
  2. import { OverlayContainer } from "@angular/cdk/overlay";
  3. import { Identifiers } from "@angular/compiler";
  4. import { stagger } from "@angular/animations";
  5. import { environment } from 'src/environments/environment';
  6.  
  7. @Component({
  8. selector: "app-root",
  9. templateUrl: "./app.component.html",
  10. styleUrls: ["./app.component.scss"]
  11. })
  12. export class AppComponent {
  13.  
  14. constructor(private oc: OverlayContainer) {
  15. //接收一个provider数组
  16. const injector = ReflectiveInjector.resolveAndCreate([
  17. {
  18. provide: Person, useClass:Person
  19. },
  20. {
  21. provide: Address, useFactory: ()=>{
  22. if(environment.production){
  23. return new Address("北京", "背景", "朝阳区", "xx街道xx号");
  24. }else{
  25. return new Address("西藏", "拉萨", "xx区", "xx街道xx号");
  26. }
  27. }
  28. },
  29. {
  30. provide: Id, useFactory:()=>{
  31. return Id.getInstance('idcard');
  32. }
  33. }
  34. ]);
  35. const person = injector.get(Person);
  36. console.log(JSON.stringify(person));
  37. }
  38.  
  39. }
  40.  
  41. class Id {
  42. static getInstance(type: string): Id {
  43. return new Id();
  44. }
  45. }
  46.  
  47. class Address {
  48. provice:string;
  49. city:string;
  50. district:string;
  51. street:string;
  52. constructor(provice, city, district, street) {
  53. this.provice=provice;
  54. this.city=city;
  55. this.district=district;
  56. this.street=street;
  57. }
  58. }
  59.  
  60. class Person {
  61. id: Id;
  62. address: Address;
  63. constructor(@Inject(Id) id, @Inject(Address )address) {
  64. this.id = id;
  65. this.address = address;
  66. }
  67. }

可以看到控制台打印出person信息。

简写:

  1. // {
  2. // provide: Person, useClass:Person
  3. // },
  4. Person, //简写为Person

在Angular框架中,框架做了很多事,在provider数组中注册的东西会自动注册到池子中。

  1. @NgModule({
  2. imports: [HttpClientModule, SharedModule, AppRoutingModule, BrowserAnimationsModule],
  3. declarations: [components],
  4. exports: [components, AppRoutingModule, BrowserAnimationsModule],
  5. providers:[
  6. {provide:'BASE_CONFIG',useValue:'http://localhost:3000'}
  7. ]
  8. })
  1. constructor( @Inject('BASE_CONFIG') config) {
  2. console.log(config); //控制台打印出http://localhost:3000
  3. }

Angular默认都是单例,如果想要每次注入都是一个新的实例。有两种方法。

一,return的时候return一个方法而不是对象。

  1. {
  2. provide: Address, useFactory: ()=>{
  3. return ()=>{
  4. if(environment.production){
  5. return new Address("北京", "背景", "朝阳区", "xx街道xx号");
  6. }else{
  7. return new Address("西藏", "拉萨", "xx区", "xx街道xx号");
  8. }
  9. }
  10. }
  11. },

二、利用父子Injector。

  1. constructor(private oc: OverlayContainer) {
  2. //接收一个provider数组
  3. const injector = ReflectiveInjector.resolveAndCreate([
  4. Person,
  5. {
  6. provide: Address, useFactory: ()=>{
  7. if(environment.production){
  8. return new Address("北京", "背景", "朝阳区", "xx街道xx号");
  9. }else{
  10. return new Address("西藏", "拉萨", "xx区", "xx街道xx号");
  11. }
  12. }
  13. },
  14. {
  15. provide: Id, useFactory:()=>{
  16. return Id.getInstance('idcard');
  17. }
  18. }
  19. ]);
  20.  
  21. const childInjector = injector.resolveAndCreateChild([Person]);
  22.  
  23. const person = injector.get(Person);
  24. console.log(JSON.stringify(person));
  25. const personFromChild = childInjector.get(Person);
  26. console.log(person===personFromChild); //false
  27. }

子注入器当中没有找到依赖的时候会去父注入器中找

以上就是详解Angular依赖注入的详细内容,更多关于Angular的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号