2016-09-18 10 views
0

ユーザがログインボタンをクリックすると、別のルートに移動しようとしています。しかし、私は何が悪くなったのか理解できません。以下は私のログインコンポーネントです。ここでangular2はルータでナビゲートできません

import { Component, OnInit } from '@angular/core'; 
 
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2'; 
 
import { Router, ActivatedRoute } from '@angular/router'; 
 

 
@Component({ 
 
    selector: 'app-login', 
 
    templateUrl: './login.component.html', 
 
    styleUrls: ['./login.component.css'] 
 
}) 
 

 

 
export class LoginComponent implements OnInit { 
 

 
    constructor(public af: AngularFire, private router: Router) { 
 
    this.af.auth.subscribe(auth => console.log(auth)); 
 
    } 
 

 
    login() { 
 
    this.af.auth.login({ 
 
     provider: AuthProviders.Google, 
 
     method: AuthMethods.Popup, 
 
    }).then(function(res) { 
 
     console.log('login success'); 
 
     console.log(res.uid); 
 
     this.router.navigateByUrl('/main') // .then(r => console.log('then: ' + r)) 
 
     .then(function(resw) { 
 
     console.log('has redirect'); 
 
     }) 
 
     .catch(err => console.error(err)) ; 
 
     console.log('afterward'); 
 
    }).catch(err => console.error(err)); 
 
    } 
 

 
    overrideLogin() { 
 
    this.af.auth.login({ 
 
     provider: AuthProviders.Anonymous, 
 
     method: AuthMethods.Anonymous, 
 
    }); 
 
    } 
 

 
    ngOnInit() { 
 
    } 
 

 
}
<p> 
 
    login works! 
 
</p> 
 

 
<button (click)="login()">Login With Google</button> 
 
<button (click)="overrideLogin()">Login Anonymously</button>

私のルートである:ここでは

import { LoginComponent } from './login/login.component'; 
import { MainComponent } from './main/main.component'; 
import { ModuleWithProviders } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router' ; 

const APP_ROUTES: Routes = [ 
    { path: 'login', component: LoginComponent }, 
    { path: '', pathMatch: 'full', redirectTo: '/login'}, 
    { path: 'main', component: MainComponent } 
]; 

export const appRoutingProviders: any[] = [ 

]; 

export const APP_ROUTES_PROVIDER: ModuleWithProviders = RouterModule.forRoot(APP_ROUTES); 

は@NgModuleです:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 

// import routing 
import { appRoutingProviders, APP_ROUTES_PROVIDER } from './app.routes' ; 



@NgModule({ 
    declarations: [ 
    AppComponent, 
    LoginComponent, 
    MainComponent 
    ], 
    imports: [ 
    BrowserModule, 
    // AngularFireModule.initializeApp(firebaseConfig, myFirebaseAuthConfig), // angularfire setup 
    FormsModule, 
    HttpModule, 
    APP_ROUTES_PROVIDER 
    ], 
    providers: [appRoutingProviders], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

私は、次のエラーを得ました。誰かが私にこれを修正する方法を教えてもらえますか?すべての設定が正しいよう

TypeError: this is null Stack trace: LoginComponent</LoginComponent.prototype.login/<@http://localhost:4200/main.bundle.js:70294:13 Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:69125:19 NgZoneImpl/this.inner<[email protected]://localhost:4200/main.bundle.js:56178:28 Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:69124:19 Zone$1</Zone</[email protected]://localhost:4200/main.bundle.js:69018:24 scheduleResolveOrReject/<@http://localhost:4200/main.bundle.js:69384:52 Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:69158:23 NgZoneImpl/this.inner<[email protected]://localhost:4200/main.bundle.js:56169:28 Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:69157:23 Zone$1</Zone</[email protected]://localhost:4200/main.bundle.js:69058:28 [email protected]://localhost:4200/main.bundle.js:69290:25 ZoneTask/[email protected]://localhost:4200/main.bundle.js:69230:25

+0

を例えば? '@ NgModule'も表示します。 – micronyks

+0

http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callbackタイプのスクリプトで太い矢印 '=>'を使用すると、 'this'がキャッシュされます。すなわち、 ' this.af.auth.login({ プロバイダ:AuthProviders.Google、 メソッド:AuthMethods.Popup、 })then((res)=> {}) ' – shusson

+0

@micronyks私はルートとNgModuleも追加しました –

答えて

1

アロー機能(()=>)は、問題を解決します。

login() { 
    this.af.auth.login({ 
     provider: AuthProviders.Google, 
     method: AuthMethods.Popup, 
    }).then((res)=> {     //<----changed this line 
     console.log('login success'); 
     console.log(res.uid); 
     this.router.navigateByUrl('/main') // .then(r => console.log('then: ' + r)) 
     .then((resw)=> {     //<----changed this line 
     console.log('has redirect'); 
     }) 
     .catch(err => console.error(err)) ; 
     console.log('afterward'); 
    }).catch(err => console.error(err)); 
    } 
0

this JavaScriptで特別では、それは関数が呼び出さHow to access the correct `this` context inside a callback?を見たかによって決まります。タイプスクリプトで

あなたが脂肪の矢印=>を使用している場合、それはあなたのためにこれをキャッシュだろうが、あなたのアプリに定義されたルートを表示することができますthis.af.auth.login({}).then((res) => {})

関連する問題