angular - 后台地理定位: Subscribe does not exist on type Promise<any>

标签 angular ionic-framework ionic3 ionic4

我正在使用 ionic 4,并尝试从 GPS 获取用户位置,特别是每隔 1 小时间隔一次。所以这是一个每小时跟踪用户位置的应用程序。

我使用了 Ionic 4 后台位置插件的官方网站文档,并找到了安装此插件的代码,该代码表示​​即使应用程序退出,此插件也会运行,所以我希望这是我所需要的。

我使用以下代码将后台地理位置插件安装到 Ionic 4 应用程序:

ionic cordova plugin add cordova-plugin-mauron85-background-geolocation@alpha
npm install @ionic-native/background-mode

但是当我尝试测试它之后,在我的 app.component.ts 主文件中,代码失败了!

this.platform.ready().then(() => { 

      this.statusBar.styleDefault();
      this.splashScreen.hide();

      //setting options for backgroun-geolocatuion
      const config: BackgroundGeolocationConfig = {
        desiredAccuracy: 10,
        stationaryRadius: 20,
        distanceFilter: 30,
        debug: true, 
        stopOnTerminate: false,  
     };



      this.backgroundGeolocation.configure(config)
      .subscribe((location: BackgroundGeolocationResponse) => {

      // ERROR comes here ==> *subscribe* does not exist on type Promise<any> ???  

        alert(location.longitude);  
      });

      this.backgroundGeolocation.start();

    });

它显示此错误:

 // ERROR comes here ==> Subscribe does not exist on type Promise<any>

由于此错误,我无法获取返回的数据作为订阅数据。我需要每 1 小时间隔的用户位置数据(即使用户是否移动),但此订阅显示了我上面所述的错误。

如果我将 subscribe 更改为 then,那么它会返回一次数据,这也是未定义的。

我需要此代码与订阅一起运行,以每小时定期跟踪用户信息。

我已经导入了代码上方和主模块文件中的所有提供程序和构造函数。

进口:

app/app.component.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    BackgroundGeolocation, 
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

最佳答案

这似乎是版本不匹配——大量版本和相互冲突的文档。经过多次尝试错误,

对于 ionic 3 这对我有用......

$ ionic cordova plugin add <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187b776a7c776e793568746d7f71763575796d6a7776202d357a797b737f6a776d767c357f7d7774777b796c717776582a362a362d" rel="noreferrer noopener nofollow">[email protected]</a>
$ npm install --save @ionic-native/background-geolocation@3

对于 ionic 4 尝试一下,它可能会对您有所帮助。

关于angular - 后台地理定位: Subscribe does not exist on type Promise<any>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683593/

相关文章:

javascript - 无法读取未定义的属性 'scrollToBottom'”

angular - npm 运行 ionic :build --prod giving strange error

html - Ios 内容页面上的 Ionic 自动向下滑动

angular - 类型 'Observable' 缺少类型 'User' 的以下属性

javascript - Ionic 中的动态形式并获取 ng-model 的值

angularjs - 将youtube视频嵌入到iFrame中

angular - 防止 Ionic 中出现重复的 Toast 消息

angular - 如何在 Angular 2 中添加条件区分大小写的属性?

javascript - 即使页面刷新也永久更改数组

css - 为什么我的媒体查询没有改变这个元素的背景颜色?