简体   繁体   English

在angular2中将服务注入服务

[英]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: 我尝试了什么:

  1. Putting another root component on top the the current root component to inject the BroadcastService there, same result 将另一个根组件放在当前根组件的顶部以向其注入BroadcastService ,结果相同
  2. Adding/Removing the @Inject to the constructor parameter, no changes 添加/删除@Inject到构造函数参数,没有更改
  3. Begging and crying, didn't help 乞求和哭泣,没有帮助

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM