As we learned in my previous article, we can now use the inject() function inside components, directives, and pipes. Here are the reasons I chose it over the constructor for dependency injection. The first thing I noticed is that it is cleaner and more readable:
Next, the provider’s type is automatically inferred:
Additionally, directiveInject() is used in the compiled code anyway:
Lastly, the Angular team appears to be heading that way. We can see the inject() usage in the new NgOptimizedImage directive, for example:
My main concern is consistency. I’d like to see an eslint plugin that helps with that.
Follow me on Medium or Twitter to read more about Angular and JS!