【发布时间】:2016-07-21 08:56:57
【问题描述】:
我想将服务注入另一个服务。我在注入标准 Angular 服务(Http 等)时没有任何问题,但是当我尝试注入自己的服务时遇到异常。
例子:
我的服务:
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());
}
}
另一个服务:
import {Injectable} from 'angular2/core';
@Injectable()
export class AnotherService {
constructor() { }
get() { return 'hello'); }
}
当我尝试使用 MyService 时,我得到 EXCEPTION: No provider for AnotherService!
我尝试使用constructor(private anotherService: AnotherService),仍然抛出异常。
谢谢!
【问题讨论】:
标签: javascript dependency-injection angular ionic2