使用index.ts文件导出类会导致注入的构造函数中的undefined [英] Using index.ts file to export class causes undefined in injected constructor

查看:246
本文介绍了使用index.ts文件导出类会导致注入的构造函数中的undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用index.ts文件封装出口,如角度2样式指南中所述( https://github.com/mgechev/angular2-style-guide/blob/master/old/README.md#directory-structure )。 />
这在我写的应用程序中运行良好,但出于某种原因,在一个服务中,我试图注入另一个服务,这会导致一个奇怪的错误。

Im using an index.ts file to encapsulate exports as mentioned in the angular 2 style guide (https://github.com/mgechev/angular2-style-guide/blob/master/old/README.md#directory-structure).
This worked fine across the app Im writing, but for some reason in one service that im trying to inject into another service this causes a strange Error.

导出的类:

import {Injectable} from "angular2/core";
@Injectable()
export class UserIds{
   private _signature_id:string;
   private _role_id:number;
   get signature_id():string{
      return this._signature_id;
   }
   set signature_id(id:string){
      this._signature_id = id;
   }
   get role_id():number{
      return this._role_id;
   }
   set role_id(id:number){
      this._role_id = id;
   }
}

index.ts文件:

The index.ts file:

export {Midiate} from "./midiate.service/midiate.service";
export {HttpRest} from "./http_rest.service/http_rest.service";
export {UserIds} from "./user_ids.service/user_ids.service"

导致错误的代码(导入文件):

The code that caused the Error (the importing file):

import {UserIds} from "../index";
import {Http} from 'angular2/http';
@Injectable()
export class HttpRest{
 constructor(
    public _http: Http,
    public userIdsx: UserIds
 ){}
...
}

浏览器抛出的错误:

EXCEPTION: Cannot resolve all parameters for 'HttpRest'(Http, undefined). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'HttpRest' is decorated with Injectable.

正如您所见,在构造函数的参数中未定义UserIds类。

As you can see the UserIds class is undefined in the params of the constructor.

将UserIds导入更改为源文件修复了问题:

Changing the UserIds import to the source file fixed the problem:

import {UserIds} from "../user_ids.service/user_ids.service";

我仍然希望使用index.ts保持较旧的样式,因为所有其他服务和组件在我的应用程序中,也明白为什么会发生这种情况。

Still I want to keep the older style on this using the index.ts as all other services and components in my app, and also understand why did this happen.

推荐答案

看起来像是将输出放在index.ts中的顺序确实很重要!不确定它是否是一个bug但是无论如何...

Looks like the order you put your exports in the index.ts does matter! Not sure if it's a bug or not but anyway...

用元数据装饰的类应该在index.ts的顶部如果其中一个注入另一个,则另一个应该高于一个。

Classes decorated with metadata should be at the top of the index.ts If one of them injects another, the "another" should be above the "one".

这篇关于使用index.ts文件导出类会导致注入的构造函数中的undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆