2016-04-27 34 views
1

我翻譯谷歌登錄JavaScript打字稿,它的作品,問題。角2路由器更改url無需重裝

我在做的是當我在谷歌登錄功能裏調用_router變量attachSignin()瀏覽器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)); 
      }); 

    } 

} 

回答

2

你「提供」 ROUTER_PROVIDERS其他地方?除了在Login組件中,可能在bootstrap(...[ROUTER_PROVIDERS])?因爲你可以在整個應用程序中只提供一次。如果多次提供它,您將在路由中看到奇怪的行爲而不會出錯。

0

我希望它可以拋出

this.zoneImpl.run(() => this._router.navigate(['Polls'])); 

如果你改變這一切

function() 

() => 

,並使用this代替self,擺脫self完全應該工作。