Add services to another angular 2 service

I tried to insert one service into another using DI angular 2

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 (?). Make sure they all have valid type or annotations.

I checked DI with normal components and it works fine. But when I bring him to the service. It just doesn't work.

+4
source share
2 answers

In angular 2, you need to inform angular about your service. To do this, you need to mark the service as Injectable.

HTTPService

import {Injectable} from 'angular2/angular2';

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

Currentblog

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

export class CurrentBlog{

    constructor(public httpService:HttpService){}
}
+1
source

In this case, just DI will not be enough. This link below refers to the same issue:

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

, :

TypeScript , emitDecoratorMetadata. , . TypeScript , , /, . , , .

, Metadta Generations, :

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

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

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

constructor(public httpService:HttpService){}
}
0

Source: https://habr.com/ru/post/1617870/


All Articles