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:

Using constructor
Using inject()

Next, the provider’s type is automatically inferred:

Auto inferred

Additionally, directiveInject() is used in the compiled code anyway:

Compiled code

Lastly, the Angular team appears to be heading that way. We can see the inject() usage in the new NgOptimizedImage directive, for example:

NgOptimizedImage

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!

--

--

As part of the new standalone feature, Angular v14.2.0 introduces a new method to use the Router API that doesn’t require using RouterModule.

Using the RouterModule, we pass extra options through a config object, for example:

We can now use these options by using feature functions:

The significant benefit of using this approach is that these features are now tree-shakable.

In tests, we can use the provideRouterForTesting() function as follows:

Follow me on Medium or Twitter to read more about Angular and JS!

--

--

Netanel Basal

Netanel Basal

A FrontEnd Tech Lead, blogger, and open source maintainer. The founder of ngneat, husband and father.