2017-04-19 79 views
0

我一直對Facebook登錄我的離子2應用 (使用本教程:https://ionicthemes.com/tutorials/about/ionic2-facebook-login離子2:Facebook的登錄 - >錯誤:NavController(TypeScipt)不提供

但現在我得到一個奇怪的錯誤:

RunTimeError Error in :0:0 caused by: No provider for NavController

app.component.ts:

import { Component } from '@angular/core'; 
import { Platform } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { NativeStorage } from '@ionic-native/native-storage'; 


import { TabsPage } from '../pages/tabs/tabs'; 
import { WelcomePage } from '../pages/welcome/welcome'; 
import { DetailPage } from '../pages/detail/detail'; 


@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    rootPage: any = WelcomePage; 

    constructor(NativeStorage: NativeStorage, platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { 
     platform.ready().then(() => { 
      // Here we will check if the user is already logged in 
      // because we don't want to ask users to log in each time they open the app 
      let env = this; 
      NativeStorage.getItem('user') 
       .then((data) => { 
        // user is previously logged and we have his data 
        // we will let him access the app 
        this.rootPage = DetailPage; 
        splashScreen.hide(); 
       }, (error) => { 
        //we don't have the user data so we will ask him to log in 
        this.rootPage = WelcomePage; 
        splashScreen.hide(); 
       }); 

      statusBar.styleDefault(); 
     }); 
    } 
} 

welcome.ts:

import { Component } from '@angular/core'; 
import { Facebook, NativeStorage } from 'ionic-native'; 
import { NavController } from 'ionic-angular'; 
import { DetailPage } from '../detail/detail'; 
import { ViewChild } from '@angular/core'; 

@Component({ 
    selector: 'page-welcome', 
    templateUrl: 'welcome.html' 
}) 
export class WelcomePage { 
    rootPage: any = WelcomePage; 
    @ViewChild('navRoot') navCtrl: NavController; 
    FB_APP_ID: number = 123456789; 

    constructor() { 
     Facebook.browserInit(this.FB_APP_ID, "v2.8"); 
    } 

    doFbLogin() { 
     let permissions = new Array(); 
     let nav = this.navCtrl; 
     //the permissions your facebook app needs from the user 
     permissions = ["public_profile"]; 


     Facebook.login(permissions) 
      .then(function (response) { 
       let userId = response.authResponse.userID; 
       let params = new Array(); 

       //Getting name and gender properties 
       Facebook.api("/me?fields=name,gender", params) 
        .then(function (user) { 
         user.picture = "https://graph.facebook.com/" + userId + "/picture?type=large"; 
         //now we have the users info, let's save it in the NativeStorage 
         NativeStorage.setItem('user', 
          { 
           name: user.name, 
           gender: user.gender, 
           picture: user.picture 
          }) 
          .then(function() { 
           nav.push(DetailPage); 
          }, function (error) { 
           console.log(error); 
          }) 
        }) 
      }, function (error) { 
       console.log(error); 
      }); 
    } 
} 

有誰知道如何解決這個問題的想法? 非常感謝!

回答

0

不能在app.component.ts或根應用頁面導入NavController。

選項1:

設法得到它使用ViewChild

給元素ID root-nav

<ion-nav #navRoot [root]="rootPage"></ion-nav> 

在組件:

import {ViewChild} from '@angular/core'; 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    rootPage: any = WelcomePage; 
    @ViewChild('navRoot') navCtrl:NavController; 

    constructor(nativeStorage: NativeStorage, platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { //remove navcontroller injected 
     platform.ready().then(() => { 
      // Here we will check if the user is already logged in 
      // because we don't want to ask users to log in each time they open the app 
      let env = this; 
      //... 

選項2:

從你app.component.ts代碼,你實際上不需要進口NavController在所有如果你的HTML模板只包含,

<ion-nav [root]="rootPage"></ion-nav> 

所需的頁面簡單地設置rootPage

nativeStorage.getItem('user') 
       .then((data) => { 
        // user is previously logged and we have his data 
        // we will let him access the app 
        this.rootPage = DetailPage; 
        splashScreen.hide(); 
       }, (error) => { 
        //we don't have the user data so we will ask him to log in 
        this.rootPage = WelcomePage; 
        splashScreen.hide(); 
       }); 

旁註:最好使用()=>{}箭頭功能的回調,而不是在第二個變量保存環境。

+0

感謝您的答覆,但誤差保持。 我已添加 從'@ angular/core'導入{ViewChild}; rootPage:any = WelcomePage; @ViewChild('navRoot')navCtrl:NavController;到welcome.ts 和改變<離子NAV #navRoot [根] = 「rootPage」> – dzitrus

+0

你刪除構造參數的navcontroller? –

+0

是的,但其餘的代碼不再工作。我在哪裏把我在app.components.ts中的代碼放在哪裏? – dzitrus