Aurelia中的打字稿自动注入

时间:2015-12-31 01:57:45

标签: javascript typescript aurelia

我是Typescript和Aurelia的新手。 我试图在VS2015 ASP.NET MVC 6项目中使@autoinject装饰器工作。

这是我的代码

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/")...
       }
}

当我运行这个时,我得到一个错误,说这个.http是未定义的。

我相信我需要添加TypeScript的emitDecoratorMetadata标志,但我不知道如何。

我尝试将tsconfig.json文件添加到我的项目中并在编译器选项中设置该标志,但后来我得到了一堆错误(重复标识符)。 我该如何解决这些错误。我是否需要在"文件"?中添加内容?到底是什么?

这是我的config.js文件

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

  map: {
    "aurelia-bootstrapper": "npm:aurelia-bootstrapper@1.0.0-beta.1",
    "aurelia-framework": "npm:aurelia-framework@1.0.0-beta.1.0.7",
    "aurelia-http-client": "npm:aurelia-http-client@1.0.0-beta.1",
    "typescript": "npm:typescript@1.7.5",
     ....
  }
  });

2 个答案:

答案 0 :(得分:5)

@autoInject()如何工作?

  

在您需要了解TypeScript的emitDecoratorMetadata标志之前,会让TypeScript编译器对Metadata Reflection API进行填充,并为已转换的TypeScript代码添加特殊的装饰器定义。

     

Aurelia的@autoInject()装饰器使用TypeScript装饰器创建的类型元数据,并以与@inject(...)装饰器相同的方式将其应用于类。

尝试如下,您需要在类型脚本的compilerOptions中启用新选项。

TS配置:

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

摘自文章的截图:

enter image description here

关于emitDecoratorMetadata的文章:
http://blog.durandal.io/2015/05/06/getting-started-with-aurelia-and-typescript/ http://www.danyow.net/inversion-of-control-with-aurelia-part-2/

可用类型脚本选项:
https://github.com/Microsoft/TypeScript/wiki/Compiler-Options

  

您可以使用Gulp-Typescript以及Gulp选项

来实现

选项:https://github.com/ivogabe/gulp-typescript#options
GitHub问题主题:https://github.com/ivogabe/gulp-typescript/issues/100

Gulp Code代码段:     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: 'commonjs',
      emitDecoratorMetadata: true,
      typescript: typescript
    }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(paths.output));
});

答案 1 :(得分:0)

@autoinject& @inject包含?

根据dependency-injection框架的aurelia库。

    /**
    * 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;
        }
      };
    }
  

来源网址:https://github.com/aurelia/dependency-injection/blob/master/src/injection.js