最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript autoinject in Aurelia - Stack Overflow

programmeradmin0浏览0评论

I am new to Typescript and Aurelia. I am try to make @autoinject decorator work in VS2015 ASP.NET MVC 6 project.

Here is my code

import {autoinject} from "aurelia-framework";
import {HttpClient} from "aurelia-http-client";

@autoinject()
export class App {
       http: HttpClient;
       constructor(httpClient: HttpClient) {
          this.http = httpClient;
       }

       activate() {
          this.http.get("/api/test/")...
       }
}

when I run this I get an error saying this.http is undefined.

I believe I need to add TypeScript's emitDecoratorMetadata flag but I don't know how.

I have tried adding tsconfig.json file to my project and set that flag in piler options but then I get a bunch of errors (duplicate identifier). How can I fix these errors. Do I need to add something to "files"? What exactly?

Here is my config.js file

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "typescript",
  paths: {
    "npm:*": "jspm_packages/npm/*",
    "github:*": "jspm_packages/github/*"
  },

  map: {
    "aurelia-bootstrapper": "npm:[email protected]",
    "aurelia-framework": "npm:[email protected]",
    "aurelia-http-client": "npm:[email protected]",
    "typescript": "npm:[email protected]",
     ....
  }
  });

I am new to Typescript and Aurelia. I am try to make @autoinject decorator work in VS2015 ASP.NET MVC 6 project.

Here is my code

import {autoinject} from "aurelia-framework";
import {HttpClient} from "aurelia-http-client";

@autoinject()
export class App {
       http: HttpClient;
       constructor(httpClient: HttpClient) {
          this.http = httpClient;
       }

       activate() {
          this.http.get("/api/test/")...
       }
}

when I run this I get an error saying this.http is undefined.

I believe I need to add TypeScript's emitDecoratorMetadata flag but I don't know how.

I have tried adding tsconfig.json file to my project and set that flag in piler options but then I get a bunch of errors (duplicate identifier). How can I fix these errors. Do I need to add something to "files"? What exactly?

Here is my config.js file

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "typescript",
  paths: {
    "npm:*": "jspm_packages/npm/*",
    "github:*": "jspm_packages/github/*"
  },

  map: {
    "aurelia-bootstrapper": "npm:[email protected]",
    "aurelia-framework": "npm:[email protected]",
    "aurelia-http-client": "npm:[email protected]",
    "typescript": "npm:[email protected]",
     ....
  }
  });
Share Improve this question asked Dec 31, 2015 at 1:57 partyelitepartyelite 8922 gold badges15 silver badges27 bronze badges 1
  • please update the full error. mean while answered as per your question. – Venkat.R Commented Dec 31, 2015 at 4:12
Add a ment  | 

2 Answers 2

Reset to default 5

How does @autoInject() work?

Before you need to aware of TypeScript's emitDecoratorMetadata flag causes the TypeScript piler to polyfill the Metadata Reflection API and add a special decorator definition to the transpiled TypeScript code.

Aurelia's @autoInject() decorator consumes the type metadata created by TypeScript's decorator and applies it to the class in the same way that the @inject(...) decorator does.

Try like below and you need to enable the new option in the pilerOptions of Type Script.

TS Configuration:

{
    "version": "1.5.1",
    "pilerOptions": {
        "target": "es5",
        "module": "amd",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": false,
        "noLib": true,
        "emitDecoratorMetadata": true
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "files": [
        // ...
    ]
}

Snippet Screenshot from Article:

Article about emitDecoratorMetadata:
http://blog.durandal.io/2015/05/06/getting-started-with-aurelia-and-typescript/ http://www.danyow/inversion-of-control-with-aurelia-part-2/

Available Type Script Options:
https://github./Microsoft/TypeScript/wiki/Compiler-Options

You can do it with Gulp-Typescript as well with Gulp option

Options: https://github./ivogabe/gulp-typescript#options
GitHub Issue Thread: https://github./ivogabe/gulp-typescript/issues/100

Gulp Code Snippet: gulp.task('build-ts', [], function() {

  return gulp.src(paths.typescript)
    .pipe(plumber())
    .pipe(changed(paths.output, {extension: '.js'}))
    .pipe(sourcemaps.init())
    .pipe(ts({
      declarationFiles: false,
      noExternalResolve: true,
      target: 'es5',
      module: 'monjs',
      emitDecoratorMetadata: true,
      typescript: typescript
    }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(paths.output));
});

What exactly @autoinject & @inject contains ?

As per dependency-injection Library of the aurelia Framework.

    /**
    * Decorator: Directs the TypeScript transpiler to write-out type metadata for the decorated class.
    */
    export function autoinject(potentialTarget?: any): any {
      let deco = function(target) {
        target.inject = metadata.getOwn(metadata.paramTypes, target) || _emptyParameters;
      };

      return potentialTarget ? deco(potentialTarget) : deco;
    }

    /**
    * Decorator: Specifies the dependencies that should be injected by the DI Container into the decoratored class/function.
    */
    export function inject(...rest: any[]): any {
      return function(target, key, descriptor) {
        // if it's true then we injecting rest into function and not Class constructor
        if (descriptor) {
          const fn = descriptor.value;
          fn.inject = rest;
        } else {
          target.inject = rest;
        }
      };
    }

Source URL: https://github./aurelia/dependency-injection/blob/master/src/injection.js

发布评论

评论列表(0)

  1. 暂无评论