当从Angular的@CanActivate
挂钩访问该服务时,我无法找到一种访问服务并正确配置其依赖关系的方法。
在实例化组件之前,我需要先检查用户是否已通过身份验证。我的钩子看起来像:
import {AuthenticationService} from '../authentication/authentication.service';
.................
@CanActivate((next, prev) => {
var injector = Injector.resolveAndCreate([
AuthenticationService
])
var authService = injector.get(AuthenticationService);
return authService.getUser();
})
export class ShellComponent { }
我得到的错误是EXCEPTION: No provider for Http! (AuthenticationService -> Http)
。应该注意的是,当应用程序被引导时会注入HTTP_PROVIDERS
。
我的身份验证服务如下:
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
@Injectable()
export class AuthenticationService {
constructor(private _http:Http) {}
getUser() {
var resolver = function(resolve, reject) {
setTimeout(function doneCounting() {
resolve(true);
}, 5000);
}
return new Promise<Boolean>(resolver);
}
}
任何帮助或见解都将不胜感激。
答案 0 :(得分:3)
您只需要将此依赖项添加到resolveAndCreate,因此您的代码将为:
var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AuthenticationService])