角度 2 路由器更改 URL 而不重新加载
angular 2 router change url without reload
我将谷歌登录JavaScript翻译成打字稿,它有效,问题。
当我在谷歌登录函数中调用_router变量时attachSignin()
浏览器网址正确更改但不重定向页面时,它只是停留在那里并且没有给出错误。
我已经尝试添加区域,但没有发生同样的事情,这是代码
这是我的代码
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])
? 因为您只能在整个应用程序中提供一次。如果多次提供它,您将在路由中看到奇怪的行为而没有错误。
我希望这会抛出
this.zoneImpl.run(() => this._router.navigate(['Polls']));
如果全部更改
function ()
自
() =>
并使用this
而不是self
并完全摆脱self
它应该有效。
相关文章:
- 创建新数据和加载现有数据需要单独的视图吗
- 钛 - HTTPClient:打开新的控制器加载
- 保持 JavaScript 在浏览器控制台中运行,即使在新的页面加载之后也是如此
- 路由到新页面时加载不起作用
- 如何在新窗口完成加载后执行某些内容
- 使用新标题重新加载 html 页面
- Primefaces在打开新页面时加载数据消息
- Wordpress音频播放器和平滑状态.在新页面重新加载播放器时调用什么回调函数
- 用新数据重新加载renderer3d
- 文本不被渲染后,新的页面加载- JQuery移动
- 需要在新标签中加载pdf/doc文件,而不是下载,只是想在浏览器中阅读
- 用新参数重新加载页面
- Js和CSS不检测新页面何时加载
- 在新页面上加载一个flash视频,它在上一页上的位置
- 在新窗口中加载代码编写器视图
- 在控制台中使用新脚本重新加载页面
- 如何在加载新页面后加载AJAX数据
- 如何使用jQuery在新窗口中加载页面,然后关闭它们
- Jquery没有看到新的HTML加载到对话框中
- 无法用新值重新加载JGrid