我创建了一个新的离子项目并为此添加了firebase,但每次我执行离子服务-c并打开DevApp时,我在控制台中看到以下错误:
离子信息:
ionic (Ionic CLI) : 4.1.2 (C:\Users\xxx\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0
科尔多瓦:
cordova(Cordova CLI):8.1.2([email protected])
Cordova Platforms : ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.0, (and 5 other plugins)
系统:
NodeJS : v8.12.0 (C:\Program Files\nodejs\node.exe)
npm : 6.4.1
OS : Windows 10
app.component.ts:
export class MyApp {
rootPage: any = HomePage;
constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public toastCtrl: ToastController, private firebase: Firebase) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
try {
this.initializeFirebase();
} catch (error) {
this.firebase.logError(error);
}
});
}
initializeFirebase() {
if (!this.platform.is("core")) {
this.firebase.subscribe("all");
this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
}
}
initializeFirebaseAndroid() {
this.firebase.getToken().then(token => { });
this.firebase.onTokenRefresh().subscribe(token => { })
this.subscribeToPushNotifications();
}
initializeFirebaseIOS() {
this.firebase.grantPermission()
.then(() => {
this.firebase.getToken().then(token => { });
this.firebase.onTokenRefresh().subscribe(token => { })
this.subscribeToPushNotifications();
})
.catch((error) => {
this.firebase.logError(error);
});
}
subscribeToPushNotifications() {
this.firebase.onNotificationOpen().subscribe((response) => {
if (response.tap) {
console.log(response.body);
//Received while app in background (this should be the callback when a system notification is tapped)
//This is empty for our app since we just needed the notification to open the app
} else {
console.log(response.body);
//received while app in foreground (show a toast)
let toast = this.toastCtrl.create({
message: response.body,
duration: 3000
});
toast.present();
}
});
}
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { Firebase } from '@ionic-native/firebase';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
Firebase
]
})
export class AppModule {}
任何人目前都有同样的问题?
您必须在provider
下将firebase添加到项目应用程序模块。您可能需要为将来安装的任何插件执行此操作。
如果你使用的是Ionic3,那就是文件app.module.ts
。
这对我有用!!在使用导入类的对象之前使用platform.ready()函数。
constructor(public qrScanner: QRScanner) {
// solve the problem - "plugin not installed".
platform.ready().then(()=>{
this.qrscanner();
})
}
希望这可以帮助...