我将谷歌登录javascript翻译成打字稿,它的确有效。
我遇到的是,当我在google登录功能attachSignin()
中调用_router变量时,浏览器网址会正确更改,但不会重定向页面,只会保留在那里并且不会出错。
我已经尝试添加区域,但是这里只是代码
这是我的代码
import {Component, NgZone} from "angular2/core";
import {ToastsManager } from 'ng2-toastr/ng2-toastr';
import {Router, ROUTER_PROVIDERS} from 'angular2/router'
// Google's login API namespace
declare var gapi: any;
@Component({
selector: "sous-app",
templateUrl: "app/login/login.html",
providers: [ToastsManager, ROUTER_PROVIDERS]
})
export class Login {
googleLoginButtonId = "google-login-button";
userAuthToken = null;
userDisplayName = "empty";
auth2 = null;
self = this;
zoneImpl: NgZone;
constructor(zone: NgZone, private _router: Router) {
this.zoneImpl = zone;
}
// Angular hook that allows for interaction with elements inserted by the
// rendering of a view.
ngAfterViewInit() {
var loginProxy = $.proxy(this.attachSignin, this);
var redirectToPolls = $.proxy(this.redirectToPolls, this);
gapi.load('auth2', function () {
// Retrieve the singleton for the GoogleAuth library and set up the client.
self.auth2 = gapi.auth2.init({
client_id: '718161509287-jdpqsuebcoteh847krjn0m1odnbo5i3q.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
loginProxy(document.getElementById('customBtn'));
});
}
attachSignin = (element) => {
var navigate = false;
console.log(element.id);
self.auth2.attachClickHandler(element, {},
(googleUser) => { {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
//HERE I WANT TO REDIRECT ROUTER
this.zoneImpl.run(() => this._router.navigate(['Polls']));
console.log(googleUser.getAuthResponse().id_token);
}, function (error) {
alert(JSON.stringify(error, undefined, 2));
});
}
}
答案 0 :(得分:2)
你在其他地方“提供”ROUTER_PROVIDERS
了吗?可能在Login
中,而不是在bootstrap(...[ROUTER_PROVIDERS])
组件中?因为您只能在整个应用中提供一次。如果多次提供,您将在路由中看到奇怪的行为而没有错误。
答案 1 :(得分:0)
我希望这会抛出
this.zoneImpl.run(() => this._router.navigate(['Polls']));
如果你改变所有
function ()
到
() =>
并使用this
代替self
并完全摆脱self
它应该有效。