Inject services in another service angular 2

前端 未结 2 1217
粉色の甜心
粉色の甜心 2021-01-13 08:32

I tried injecting one service in another using DI of angular 2

import {HttpService} from \'scripts/httpService\';
export class CurrentBlog{
    constructor(         


        
2条回答
  •  自闭症患者
    2021-01-13 09:08

    In angular 2 you need to make the angular injector aware of 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){}
    }
    

提交回复
热议问题