访问谷歌登录函数 Angular 2 中的构造函数参数
Access constructor parameters inside the google sign in function Angular 2
我将谷歌登录JavaScript翻译成打字稿,它有效,我遇到的问题是我无法访问谷歌登录功能中的_router变量。在 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;
constructor(
public _router: Router) { }
// 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, {},
function (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 ACCESS ROUTER
_router.navigate(['Polls']);
console.log(googleUser.getAuthResponse().id_token);
}, function (error) {
alert(JSON.stringify(error, undefined, 2));
});
}
}
将
function()
更改为() =>
self.auth2.attachClickHandler(element, {},
(googleUser) => {
相关文章:
- 使用闭包共享构造函数参数
- 当一个重要的构造函数参数丢失时应该发生什么
- 使构造函数参数具有ES6类方法的特权
- Windows.Networking.EndpointPair构造函数参数
- ES6类构造函数参数
- 访问谷歌登录函数 Angular 2 中的构造函数参数
- JavaScript-构造函数参数和原型继承
- JavaScript构造函数参数
- 避免将jquery选择器作为构造函数参数发送时内存泄漏
- 是否需要初始化在 Javascript 中作为“类”构造函数参数传递的变量
- 消费者如何在 Angular 2 中提供组件构造函数参数
- 在 JavaScript 中以编程方式设置构造函数参数
- 服务构造函数参数
- 是否可以在没有构造函数参数的情况下实例化空对象/对象
- JavaScript 构造函数参数
- 如何在 Javascript 中处理错误的构造函数参数
- 子类化 JavaScript 构造函数参数不传递给数组
- IIFE创建模式-但如何支持构造函数参数
- 从构造函数内部的JSON获取构造函数参数
- JavaScript中构造函数参数的命名约定