javascript - 在谷歌登录函数 Angular 2 中访问构造函数参数

标签 javascript angular angular2-routing google-signin

我将 google sign in javascript 翻译成 typescript,它工作正常,我遇到的问题是我无法访问 google sign in 函数中的 _router 变量。在 attachSignin() 函数中,我尝试从构造函数访问 _router: 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) => {

关于javascript - 在谷歌登录函数 Angular 2 中访问构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778158/

相关文章:

javascript - 如何在不使用 <MuiThemeProvider/> 的情况下更改 Material-UI TextField 底部颜色

javascript - Angular : Data from child undefined

angular - 直接链接到 Angular 4 中的子路由

angular - Angular4中ActivatedRoute和ActivatedRouteSnapshot有什么区别

angular2-routing - 如何最好地将租户包含在 Angular 2 路由中

javascript - 循环遍历路由以获取单个值并将其推送到变量

javascript - 修复错误: Cannot read property 'path' of undefined

javascript - 一页网站,显示/隐藏 div,菜单按钮不会保持选中状态

Javascript:动态添加文本字段和选择标签未保存

angular - 如何创建一个 Angular 库来拦截我的应用程序发出的 http 请求