2016-08-18 128 views
0

我是Ionic2的新用戶,第一次使用Firebase。當嘗試使用上述兩個示例登錄應用程序我得到一個編譯錯誤,因爲Ionic2 + Firebase應用程序編譯錯誤:TypeScript錯誤:錯誤TS2503:找不到命名空間'firebase'

打字稿錯誤:

TypeScript error: /ionic/devdactic-firebase22/node_modules/angularfire2/database/database.d.ts(9,29): Error TS2503: Cannot find namespace 'firebase'. TypeScript error: /ionic/devdactic-firebase22/node_modules/angularfire2/database/database.d.ts(10,31): Error TS2503: Cannot find namespace 'firebase'. TypeScript error: /ionic/devdactic-firebase22/node_modules/angularfire2/providers/auth.d.ts(15,40): Error TS2503: Cannot find namespace 'firebase'. TypeScript error: /ionic/devdactic-firebase22/node_modules/angularfire2/providers/auth.d.ts(16,52): Error TS2503: Cannot find namespace 'firebase'. TypeScript error:/ionic/devdactic-firebase22/node_modules/angularfire2/providers/auth.d.ts(16,92): Error TS2503: Cannot find namespace 'firebase'.

我與安裝指南AngularFire嘗試:https://github.com/angular/angularfire2/blob/master/docs/1-install-and-setup.md

也試過在我app.ts進口火力爲

import * as firebase from 'firebase';

還是沒找到任何解決方案。可能是什麼原因,我該如何解決?

我使用[email protected]^2.0.0-beta.2 和[email protected]^2.4.2。

app.ts

import {Component} from '@angular/core'; 
import {Platform, ionicBootstrap} from 'ionic-angular'; 
import {StatusBar} from 'ionic-native'; 
import {LoginPage} from './pages/login/login'; 
import * as firebase from 'firebase'; 

import {FIREBASE_PROVIDERS, 
    defaultFirebase, 
    AngularFire, 
    AuthMethods, 
    AuthProviders, 
    firebaseAuthConfig} from 'angularfire2'; 


@Component({ 
    template: '<ion-nav [root]="rootPage"></ion-nav>', 
    providers: [ 
     FIREBASE_PROVIDERS, 
     // defaultFirebase('https://loginapp-a08a6.firebaseio.com/'), 
     firebaseAuthConfig({ 
      provider: AuthProviders.Password, 
      method: AuthMethods.Password 
     }) 
    ] 
}) 
export class MyApp { 
    rootPage: any = LoginPage; 

    constructor(platform: Platform) { 
    platform.ready().then(() => { 
     StatusBar.styleDefault(); 
    }); 
    var config = { 
     apiKey: "AIzaSyBU2Vqq7AjN4rlMWjyKBbXo2RVJXAR4TeM", 
     authDomain: "loginapp-a08a6.firebaseapp.com", 
     databaseURL: "https://loginapp-a08a6.firebaseio.com", 
     storageBucket: "loginapp-a08a6.appspot.com", 
     }; 
     firebase.initializeApp(config); 
    } 
} 

ionicBootstrap(MyApp); 

login.ts:

import {Component} from '@angular/core'; 
import {NavController, AlertController, LoadingController} from 'ionic-angular'; 
import {FirebaseAuth} from 'angularfire2'; 
import {HomePage} from '../home/home'; 

@Component({ 
    templateUrl: 'build/pages/login/login.html', 
}) 
export class LoginPage { 
    // public loading: Loading; 

    constructor(public nav: NavController, 
    public auth: FirebaseAuth, 
    private alertCtrl:AlertController, 
    private loadingCtrl:LoadingController) {} 

    public registerUser(credentials) { 
    this.showLoading() 
    this.auth.createUser(credentials).then((authData) => { 
     let prompt = this.alertCtrl.create({ 
     title: 'Success', 
     subTitle: 'Your new Account was created!', 
     buttons: ['OK'] 
     }); 
     prompt.present(); 
    }).catch((error) => { 
     this.showError(error); 
    }); 
    } 

    public login(credentials) { 
    this.showLoading() 

    this.auth.login(credentials).then((authData) => { 
     this.nav.setRoot(HomePage); 
    }).catch((error) => { 
     this.showError(error); 
    }); 
    } 

    showLoading() { 
    var loader = this.loadingCtrl.create({ 
     content: 'Please wait...', 
     duration:3000 
    }); 
    loader.present(); 
    } 

    showError(text) { 
    let alert = this.alertCtrl.create({ 
     title: 'Fail', 
     subTitle: text, 
     buttons: ['OK'] 
    }); 
    alert.present(); 
    } 
} 

回答

0

有不同的設定,對於這類問題的工作答案。爲我工作的那個人是:

您可以直接在typings.json中引用包含在angularfire2節點包中的Firebase 3類型文件。 運行:

  • typings install file:node_modules/angularfire2/firebase3.d.ts --save --global
    • 這節省了參考到typings.json
    • 注:需要分型.1 +
  • typings install

這將使中的類型文件0目錄。

其他的解決方案,在GitHub上https://github.com/angular/angularfire2/issues/234

0

看看這個問題在tsconfigcompilerSettings,添加屬性 「類型」,並指定 「火力點」 的類型:

{ 
    "compilerOptions": { 
    "allowSyntheticDefaultImports": true, 
    "declaration": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "lib": [ 
     "dom", 
     "es2015" 
    ], 
    "module": "es2015", 
    "moduleResolution": "node", 
    "target": "es5", 
    "types": [ 
     "firebase" 
    ] 
    }, 
    "exclude": [ 
    "node_modules" 
    ], 
    "compileOnSave": false, 
    "atom": { 
    "rewriteTsconfig": false 
    } 
}