If Aurelia understands import, why use dependency injection?

I don’t understand .. if I can use importin Aurelia, why do I need to connect a constructor using @autoinject()and all that? I'm sure something is missing, but as far as I can tell, I can just use my imported module whenever I want.

import something from "whatever"

export class SomeViewModel {
    activate() {
        // use something
    }
}
+4
source share
2 answers

Typically, in an Aurelia application, what you importing is not an instance of a Somethingclass Something. To use everything that was imported, you need an instance of this file.

import Something from 'whatever';

let something = new Something();

Aurelia Dependency Injection, , "Inversion of Control". , ( ) , , , , .

, ( , Aurelia DI). Dependency Injection , .

--- OP ---

, Something, aurelia view model, , . Something.

, Aurelia Dependency . :

import {inject} from 'aurelia-framework';
import Something from 'somewhere';

@inject(Something)
export class Foo {
  constructor(something) {
    this.something = something;
  }
  //...
}

import Something from 'somewhere';
export class Foo {
  constructor(Something) {
    this.something = something;
  }
  //...
}

: " , , ", : ", , ".

, , aurelia DI , . - js aurelia , , ( aurelia DI). ?

. , moment, , , Aurelia. , .

+11

, Aurelia DI, . Injection Dependency . , , .

+5

All Articles