Why do you need the @ decorators for certain injectables in V2?

@IRouter, @ILogger decorators needed to inject the logger and router in V2 what is the logic behind this?

These are the parameter decorators as well as injection tokens that registers the dependencies of a class (as metadata). When a class is instantiated via DI, these dependencies are fetched from the metadata and recursively resolved in order to instantiate the class.

Sure, but why are they needed when they were not in V1 with @autoInject useage.

The @autoinject is not yet ported to Au2. We might port that.

cc: @bigopon

Considering that every view class I have uses @autoinject, I consider @autoinject as a rather basic requirement if porting from v1

1 Like

Interface types do not exist in JavaScript, these decorators allow the DI container to inject the correct instance

1 Like

When using typescript, you don’t need to explicitly annotate the types (docs)

@autoinject is called @inject in Aurelia 2:

import { inject } from 'aurelia';

@inject()
export class FileImporter {
  constructor(private fileReader: FileReader, logger: Logger) { ... }
}

If they are real types

as a matter of fact it doesnt matter you use as long as anyone of the aurelia ones is provided the deco metadata for the class is provided and DI works.

As @MaximBalaganskiy mentioned though, property injectors make use of a special Symbol wrapping a TS interface and thus making it “materialized” aka not disappear after transpilation. for these you need to be explicit and use the given injection tokens.

as for the purpose, I recommend giving this great DI docs a thorough read Dependency injection (DI) - The Aurelia Docs where all what I mentioned before plus in the “using interfaces” section you get a nice rundown. it also goes into why you’d want to do that, e.g. a plugin providing overridable named configurations

Yes, of course, sorry for being inaccurate. I often use abstract classes instead of interfaces in cases where I need dependency injection.