[英]Injecting services into services in angular2
I want to write some kind of Broadcast mechanism to dynamically create and use EventEmitter
s in my user context. 我想编写某种广播机制来动态创建和使用我的用户上下文中的
EventEmitter
。 To do so I inject the an EmitterService
为此,我注入了一个
EmitterService
@Injectable()
export class BroadcastService implements OnInit {
private _emitters: { [channel: string]: EventEmitter<any> } = {};
constructor() {}
get(channel: string): EventEmitter<any> {
if (!this._emitters[channel]) {
this._emitters[channel] = new EventEmitter();
}
return this._emitters[channel];
}
ngOnInit() { }
}
My components structure looks like this: 我的组件结构如下所示:
[root]
| |
[comp-a][comp-b]
In my root
component I inject the BroadcastService
to make sure every sub-component uses the same Broadcast Channels. 在我的
root
组件中,我注入BroadcastService
以确保每个子组件使用相同的广播信道。 In addition other services like an AuthService
are injected (in the root
too, that's basically my user context) 此外还注入了其他服务,如
AuthService
(在root
也是如此,这基本上是我的用户上下文)
@Component({
provider: [BroadcastService, AuthService]
})
The AuthService
(and others) are using the BroadcastService
for emit events: AuthService
(和其他人)正在使用BroadcastService
来发送事件:
@Injectable()
export class AuthService extends BaseService {
constructor(
http: Http,
@Inject(BroadcastService) private emitterService: BroadcastService)
{
super(http);
}
...
// Example usage of Broadcast
login(username: string, password: string) {
// do the login, when successfully:
this.emitterService.get("online").emit(true);
}
}
The error I get: EXCEPTION: Cannot resolve all parameters for 'AuthService'(Http, undefined @Inject(undefined)). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AuthService' is decorated with Injectable.
我得到的错误:
EXCEPTION: Cannot resolve all parameters for 'AuthService'(Http, undefined @Inject(undefined)). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AuthService' is decorated with Injectable.
EXCEPTION: Cannot resolve all parameters for 'AuthService'(Http, undefined @Inject(undefined)). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AuthService' is decorated with Injectable.
What I tried: 我尝试了什么:
BroadcastService
there, same result BroadcastService
,结果相同 The strangest part is, that this works with one of my other services and doesn't with the rest of them (well, I didn't try all, but those I tried didn't work). 最奇怪的部分是,这适用于我的其他服务之一,而不是与其他服务一起工作(好吧,我没有尝试所有,但我试过的那些不起作用)。 Do you have any clues what the problem could be?
你有什么线索可能是什么问题吗? Or is this maybe just a bug (I'm using angular2 in 2.0.0-beta.2)
或者这可能只是一个错误(我在2.0.0-beta.2中使用angular2)
Seems like a forwardRef
is required 好像需要
forwardRef
@Injectable()
export class AuthService extends BaseService {
constructor(
http: Http,
@Inject(forwardRef(() => BroadcastService)) private emitterService: BroadcastService)
{
super(http);
}
...
// Example usage of Broadcast
login(username: string, password: string) {
// do the login, when successfully:
this.emitterService.get("online").emit(true);
}
}
could also (or instead of) be necessary in 也可能(或代替)有必要
@Component({
provider: [BroadcastService, AuthService]
})
depending on how you code is organized. 取决于您的代码组织方式。 This is not necessary when each class is defined in its own file.
当每个类在其自己的文件中定义时,这不是必需的。
See also Angular 2 error: 另见Angular 2错误:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.