Ionic 4 native plugin not working

2020-06-23 07:54发布

问题:

I am facing a problem with Ionic 4 native plugins. I have installed ionic 4 Cordova native plugin "Geolocation" but when I run the app into my iOS or Android device it's showing to me a blank white screen and nothing happening.

app.module.ts code:

import {
  Geolocation
} from '@ionic-native/geolocation';
@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    ComponentsModule
  ],
  providers: [
    StatusBar,
    SplashScreen, ** Geolocation ** , {
      provide: RouteReuseStrategy,
      useClass: IonicRouteStrategy
    }
  ],
  bootstrap: [AppComponent]
})

Home.ts code:

import {
  Geolocation
} from '@ionic-native/geolocation';
constructor(private geolocation: Geolocation) {}

I don't know why it's happening. But It's working on Ionic 3 project.

Anyone can give me any idea why it's happening and what I did wrong.

回答1:

I've found the solution :

  • open your package.json and update @ionic-native/geolocation to 5.0.0-beta.14 (same as @ionic-native/core)
  • npm install
  • update all your imports : import {Geolocation} from '@ionic-native/geolocation/ngx'; (in app.module.ts and in your component.ts file)