gpt4 book ai didi

javascript - Angular 2路由器无需重新加载即可更改网址

转载 作者:行者123 更新时间:2023-11-30 16:06:54 25 4
gpt4 key购买 nike

我将 google sign in javascript 翻译成 typescript,它有效,问题所在。

我遇到的问题是,当我在谷歌登录函数 attachSignin() 中调用 _router 变量时,浏览器 url 会正确更改但不会重定向页面,它只是停留在那里并且没有给出任何错误。

我已经尝试添加区域,但什么也没发生,这里是代码

这是我的代码

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));
});

}

}

最佳答案

您是否在其他任何地方“提供”ROUTER_PROVIDERS?除了在 Login 组件中,也许在 bootstrap(...[ROUTER_PROVIDERS]) 中?因为您只能在整个应用程序中提供一次。如果你多次提供它,你会在没有错误的情况下看到路由中的奇怪行为。

关于javascript - Angular 2路由器无需重新加载即可更改网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36899611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com