祝大家有美好的一天!
我正在使用创建一个网络应用程序Angular.js 1.5, 谷歌身份验证(gapi) and 用户界面路由器.
我的主要目标是初始化谷歌用户首先,使用解决方法从主要抽象状态:
$stateProvider
.state('app', {
url:'/',
templateUrl: 'layout/app-view.html',
resolve: {
googleAuth: function (User:UserService) {
return User.initCurrent();
}
}
});
此时效果非常好 - 它注入用户服务,并称其为初始化当前()方法,返回一个promise一旦当前用户被初始化,它将被完整归档。这是UserService简洁代码:
class UserService implements UserInterface {
public current:GoogleUser;
private _GoogleAuth:GoogleAuthService;
private _AppConstants;
constructor(GoogleAuth:GoogleAuthService) {
'ngInject';
this._GoogleAuth = GoogleAuth;
}
initCurrent():ng.IPromise<GoogleUser> {
return this._GoogleAuth.getCurrentUser().then((current) => {
this.current = current;
return this.current;
})
}
}
轮到时初始化当前()收到来自的承诺获取当前用户()一旦实现gapi将被加载并且谷歌验证实例将被初始化。为此我创建了一个谷歌验证服务:
class GoogleAuthService implements GoogleAuthServiceInterface {
public googleAuth;
private _AppConstants;
private deferGoogleAuthInit:ng.IDeferred<any>;
constructor(AppConstants, $log, $q:ng.IQService) {
'ngInject';
this._AppConstants = AppConstants;
this.deferGoogleAuthInit = $q.defer();
gapi.load('auth2', () => {
gapi.auth2.init({
client_id: AppConstants.googleClientId,
cookiepolicy: 'single_host_origin'
}).then((googleAuth) => {
this.googleAuth = googleAuth;
this.deferGoogleAuthInit.resolve('GoogleAuth Initialized');
})
});
}
getCurrentUser() {
return this.deferGoogleAuthInit
.promise.then(() => {
return this.googleAuth.currentUser.get()
})
}
}
问题:当我不使用解析方法来初始化用户时,并通过控制台执行相同的操作:获取 UserService 实例、initCurrentUser 等。一切正常:登录/退出、获取当前用户等。
但是,当我在解析方法中初始化当前用户时,尝试注销会导致以下错误消息:
未捕获的类型错误:无法读取 null 的属性“postMessage”
但与此同时,所有服务都正确注入,并且我收到了正确的用户实例,尽管我无法重新登录它。你有什么建议?