New StaticInjector APIs for DI in Angular 5

ref: https://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

Since version 5.0.0, ReflectiveInjector has been deprecated in favour of StaticInjectorto not depend on reflection to get hold of dependencies. We don’t really have to worry about this if we aren’t dealing with creating injectors manually (because Angular does it for us). However, if we are, for whatever reason, relying on lower level injector APIs, here’s what’s different:

  • Creating injectors is done via Injector.create()
  • Dependencies tokens have to be listed explicitly using deps property

StaticInjector doesn’t really need to resolve tokens anymore as everything is supposed to be static and passed right to the configuration. That’s why there’s not resolveAndCreate(), but a create() method. Also, instead of importing ReflectiveInjector, we’re importing Injector.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s