Aurelia Dependency Injections

Last article, we tackled Aurelia templates. In this post, we will discuss Aurelia dependency injections.

Dependency injections or DI is used by the framework to instantiate view models such as custom components, attributes or elements created as classes through the DI container. These classes are designed to be small and attain a common task. This way, the code is modularized and easy to test.

To use this in code, you must import inject library to make use of the inject() decorator.

dependency injections

For this instance, we used ES2016 and added the HttpClient inside the inject() decorator and added the http parameter to its constructor. What it does here is that the inject() decorator should pass a list of type to provide instances of. Then for each constructor parameters, there should be one argument such as http.

For ES2015, this is added by writing a static inject method to a class that returns an array of types to inject whereas TypeScript 1.5 or lower is added by writing @autoinject decorator to a class and choose the Types in the decorator call. But it’s just used on constructor’s signature.

On the next section, we will go through Aurelia custom attributes.

Subscribe Now and Upgrade Your Skills Today!
Sign up now and have the latest tech tutorials delivered straight to your mailbox.

PLUS: Get exclusive member-only discounts (up to 80% OFF!!) on our top tech courses in Udemy!
We hate spam. Your email address will not be sold or shared with anyone else.