小编NoN*_*ame的帖子

在Angular 2中的另一个服务中注入自定义服务

我想将服务注入另一个服务.我注入标准角度服务(Http等)没有任何问题,但是当我尝试注入自己的服务时,我得到了一个例外.

例:

为MyService:

import {Injectable, Inject} from 'angular2/core';
import {AnotherService} from '../../services/another.service';

@Injectable()
export class MyService {
  constructor(Inject(AnotherService) private anotherService: AnotherService) {
    console.log(this.anotherService.get());
  }
}
Run Code Online (Sandbox Code Playgroud)

AnotherService:

import {Injectable} from 'angular2/core';

@Injectable()
export class AnotherService {

   constructor() { }
   get() { return 'hello'); }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用MyService时,我得到了 EXCEPTION: No provider for AnotherService!

我尝试过使用constructor(private anotherService: AnotherService),仍然会抛出异常.

谢谢!

javascript dependency-injection ionic2 angular

5
推荐指数
1
解决办法
1409
查看次数