2016-04-27 21 views
1

私はJavaScriptをtypescriptに翻訳しています。それは問題です。角度2ルーターはリロードなしでURLを変更します

Googleのログイン機能で_router変数を呼び出すと、attachSignin()ブラウザのURLが正しく変更されますが、そこに残っているページはリダイレクトされず、エラーは発生しません。

私はすでにゾーンを追加しようとしたが、ここでのnothinはちょうど同じ起こり、これはあなたがどこか他のROUTER_PROVIDERSを「提供」しました私のコード

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

ある

コードはありますか? Loginコンポーネント以外、おそらくbootstrap(...[ROUTER_PROVIDERS])にありますか?アプリ全体に一度だけ提供できるからです。複数回指定すると、エラーのないルーティングで奇妙な動作が発生します。

0

私は、これはあなたがすべての

function() 

() => 

を変更した場合は

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

を投げ、代わりにselfthisを使用してself全くそれはすべき取り除くことを期待します作業。