Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Aurelia中的Typescript自动注入_Javascript_Typescript_Aurelia - Fatal编程技术网

Javascript Aurelia中的Typescript自动注入

Javascript Aurelia中的Typescript自动注入,javascript,typescript,aurelia,Javascript,Typescript,Aurelia,我不熟悉打字脚本和奥雷莉亚。 我试图让@autoinjectdecorator在VS2015 ASP.NETMVC6项目中工作 这是我的密码 import {autoinject} from "aurelia-framework"; import {HttpClient} from "aurelia-http-client"; @autoinject() export class App { http: HttpClient; constructor(httpCli

我不熟悉打字脚本和奥雷莉亚。 我试图让@autoinjectdecorator在VS2015 ASP.NETMVC6项目中工作

这是我的密码

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/")...
       }
}
当我运行这个时,我得到一个错误,说this.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",
     ....
  }
  });
@autoInject()是如何工作的? 在您需要知道TypeScript的
emitDecoratorMetadata
标志之前,TypeScript编译器会多填充元数据反射API,并向传输的TypeScript代码添加特殊的装饰器定义

Aurelia的@autoInject()decorator使用由TypeScript的decorator创建的类型元数据,并以与@inject(…)decorator相同的方式将其应用于类

像下面这样尝试,您需要在脚本类型的
编译器选项中启用新选项

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": [
        // ...
    ]
}
文章的片段屏幕截图:

关于emitDecoratorMetadata的文章:

可用的类型脚本选项:

您可以使用Gulp Typescript和Gulp选项来完成

选项:
GitHub发布线程:

吞咽代码片段: 吞咽任务('build-ts',[],function()){

@autoinject&@inject到底包含什么? 根据
aurelia
框架的
dependency injection

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


请更新完整的错误。请根据您的问题回答。您正在谈论注入装饰器。我的问题是关于自动注入装饰器。它们不是相同的兴趣。让我检查一下out@autoinject将decorator添加到类中,并在decorator调用中省略类型,但只在构造函数的签名上使用它们。@partyel在这里,你必须在构造函数签名中使用autoject。请注意:AutoInjectdecorator在你的类中,省略decorator调用中的类型,但只在构造函数的签名中使用它们。希望你的TypeScript>=1.5!我这样做了,看看上面的代码。我在autoinjector调用中没有任何类型,在我使用的是TypeScript 1.7。5@partyelite,检查此项以了解有关自动注入和注入的信息
    /**
    * 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;
        }
      };
    }