本文介绍了可以`deps:[]`也可以和`useClass`一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道如何使用 deps

当工厂方法需要Injected tokens时,我们需要提供它们:

  const randomFactory =(car,engine)=> {return ...}; 
...
供应商:[汽车,引擎,
{提供:'随机',
useFactory:randomFactory,
deps:[Car,Engine],
},
]

但我读过



StaticProvider



这是一种用于以静态方式配置Injector的提供程序(没有反射



根据



这是什么意思?



1)当我们将提供商传递给平台时,我们必须指定 deps 因为我们必须使用 StaticClassProvider ConstructorProvider 而不是 ClassProvider (见上图)

  platformBrowserDynamic()。bootstrapModule(AppModule,
{
提供者:[
{
提供:ElementSchemaRegistry,
useClass:CustomDomElementSchemaRegistry,
deps:[]< =============== ======此处需要
}
]
}
);

2)当我们动态创建Injector时,我们必须指定 deps 因为 Injector.create StatisProvider 数组。



例如:

  export const MyParams = new InjectionToken< string []>( 'PARAMS'); 

导出类MyService {
构造函数(@Inject(MyParams)public someParameters:string []){}
}

@Component({
选择器:'my-app',
templateUrl:'。/ app.component.html',
styleUrls:['。/ app.component.css']
})
导出类AppComponent {
name ='Angular'+ VERSION.full;

构造函数(){
const inj = Injector.create([
{provide:MyService,useClass:MyService}< ===我们会收到错误,因为我们有定义deps
])
}
}



提供商



我们通常在 @NgModule @ Component /中写提供商时使用的提供商@Directive metadatas



看看这个答案我会说<$那里不需要c $ c> deps 。我们只需要在providers数组中提供 Params ,而angular将为我们完成所有工作。






@estus表示

因为他的意思是提供商(更确切地说, ClassProvider )不是 StaticProvider






PS 您还可以阅读我关于 StaticInjector 的文章:)






I already know what/how to use deps.
It is when a factory method needs Injected tokens , so we need to supply them like :

const randomFactory = (car,engine) => { return ... };
 ...
 providers: [Car,Engine, 
             { provide: 'Random', 
               useFactory: randomFactory ,  
               deps: [Car, Engine],
             },
            ]

But I've read here :

But then I've asked in other place :

I didn't want to continue comments in two places and hence my question :

Question:

In which scenarios would I use useClass with deps ?

Also , even if I used it , say class Foo :

Class Foo
{
 constructor ( private s1:Service1 , private s2:service2){}
}

^ Which already(!) have a ctor of its own. Where would deps dependencies be injected ? ( appended to ctor??)

}

An example of scenario + code would be much appreciated.

解决方案

There are two kinds of providers:

StaticProvider and Provider

StaticProvider

It's kind of providers that are used to configure Injector in a static way(without Reflection)

According to the commit

Changelog

What does it mean?

1) When we pass provider to platform we have to specify deps because we have to use StaticClassProvider or ConstructorProvider instead of just ClassProvider (see picture above)

platformBrowserDynamic().bootstrapModule(AppModule, 
 {
   providers: [
    { 
       provide: ElementSchemaRegistry, 
       useClass: CustomDomElementSchemaRegistry, 
       deps: [] <===================== required here
    }
   ]
 }
);

2) When we create Injector dynamically we have to specify deps because Injector.create takes StatisProvider array.

For instance:

export const MyParams = new InjectionToken<string[]>('params');

export class MyService {
  constructor(@Inject(MyParams) public someParameters: string[]) {}
}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular ' + VERSION.full;

  constructor() {
      const inj = Injector.create([
      { provide: MyService, useClass: MyService  } <=== we will get an error because we have to define deps
    ])
  }
}

https://ng-run.com/edit/5Xm4jwAoXXyAIspwF571

Provider

It's kind of providers that we usually use when write providers in @NgModule or @Component/@Directive metadatas

Looking at this answer how the parameters of a forRoot() module's method is passed to a provider? I would say that deps is not required there. We only need to provide Params in providers array and angular will do all job for us.


@estus said that

because he meant Provider(more precisely ClassProvider) not StaticProvider


P.S. You can also read my article about StaticInjector :)

这篇关于可以`deps:[]`也可以和`useClass`一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 19:32