简体   繁体   English

在另一个服务角2中注入服务

[英]Inject services in another service angular 2

I tried injecting one service in another using DI of angular 2 我尝试使用角度为2的DI在另一个服务中注入一个服务

import {HttpService} from 'scripts/httpService';
export class CurrentBlog{
    constructor(public httpService:HttpService){}
}

When I do this, I get this error : 当我这样做时,我收到此错误:

Cannot resolve all parameters for CurrentBlog(?). 无法解析CurrentBlog(?)的所有参数。 Make sure they all have valid type or annotations. 确保它们都具有有效的类型或注释。

I have tested DI with normal components and it works fine. 我用正常的组件测试了DI,它工作正常。 But when I inject it in service. 但是当我注入服务时。 It simply doesn't work. 它根本不起作用。

In angular 2 you need to make the angular injector aware of your service. 在角度2中,您需要使角度注射器了解您的服务。 To do this you need to mark the service as Injectable. 为此,您需要将服务标记为“可注入”。

HttpService HttpService的

import {Injectable} from 'angular2/angular2';

@Injectable()
export class HttpService{
    ...
}

CurrentBlog CurrentBlog

import {HttpService} from 'scripts/httpService';
import {Inject} from 'angular2/core';

export class CurrentBlog{

    constructor(public httpService:HttpService){}
}

In this case just DI won't be enough. 在这种情况下,只有DI是不够的。 This link below addresses the same problem : 以下链接解决了同样的问题:

http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html

So it says : 所以它说:

TypeScript generates metadata when the emitDecoratorMetadata option is set. 设置emitDecoratorMetadata选项时,TypeScript会生成元数据。 However, that doesn't mean that it generates metadata blindly for each and every class or method of our code. 但是,这并不意味着它会盲目地为我们代码的每个类或方法生成元数据。 TypeScript only generates metadata for a class, method, property or method/constructor parameter when a decorator is actually attached to that particular code. 当装饰器实际附加到该特定代码时,TypeScript仅为类,方法,属性或方法/构造函数参数生成元数据。 Otherwise, a huge amount of unused metadata code would be generated, which not only affects file size, but it'd also have an impact on our application runtime. 否则,将生成大量未使用的元数据代码,这不仅会影响文件大小,而且还会对我们的应用程序运行时产生影响。

So to enforce Metadta Generations we can try putting either of the following two annotations : 因此,为了强制执行Metadta Generations,我们可以尝试使用以下两个注释中的任何一个:

import {HttpService} from 'scripts/httpService';
import {Inject} from 'angular2/core';
export class CurrentBlog{

constructor(@Inject(HttpService) public httpService:HttpService){}
}

Or, 要么,

import {Injectable} from 'angular2/core';
import {HttpService} from 'scripts/httpService';
@Injectable()
export class CurrentBlog{

constructor(public httpService:HttpService){}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2“服务”如何将一个服务注入另一个服务(单身人士)? - Angular2 “Services” how to @inject one service into another (singletons)? 将一项服务注入另一项服务,并可以访问角度为7的注入服务成员 - inject one service into another and have access to the injected services members angular 7 角度e2e测试:如何测试Service注入(使用)其他服务 - angular e2e testing : how to test Service inject(use) another services 在Angular 2的另一个服务中注入新的服务实例 - Inject new Instance of Service in another Service in Angular 2 在Angular 2的另一个服务中注入自定义服务 - Inject custom service in another service in Angular 2 Angular2:无法将服务注入另一个服务 - Angular2 : Unable to Inject a service into another service angular 7 如何将http服务注入另一个服务 - angular 7 how to inject http service into another service Angular 2将服务注入服务 - Angular 2 inject service into service 另一个服务中的Angular2 Inject服务创建2个实例 - Angular2 Inject service in another service creates 2 instances 如何将一个服务的数组注入 Angular 中的另一个服务的数组 - How to inject an Array from one Service into an array of another service in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM