对象不是函数。 network.ts中的错误

时间:2019-03-01 11:55:25

标签: angular typescript ionic-framework

启动ionic应用程序时,我遇到一个错误对象不是函数。也许有人知道为什么我会报错对象不是函数,错误在哪里?谢谢大家 这是用于在使用离子应用程序时检查连接性用户的服务提供商。我试图

network.ts

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Network } from '@ionic-native/network/ngx';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class NetworkService {

  info: any = {
    connected: true,
    type: "none"
  };

  disconnectSubscription: any;
  connectSubscription: any;

  private infoConnection = new Subject<any>();
  infoConnection$ = this.infoConnection.asObservable();

  constructor(
    private network: Network,
    private platform: Platform
  ) {
    this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
      this.sendStatus();
    });

    this.connectSubscription = this.network.onConnect().subscribe(() => {
      this.sendStatus();
    });
  }

  sendStatus() {
    if (this.platform.is("cordova")) {
      setTimeout(() => {
        this.info = {
          connected: this.isConnected(),
          type: this.getConnectionType()
        }
        this.infoConnection.next(this.info);
      }, 3000);
    }
  }

  isConnected() {
    if (this.platform.is("cordova")) {
      let hasConnection = this.network.type == "none" || this.network.type == 'unknown' ? false : true;
      return hasConnection;
    } else {
      return true;
    }
  }

  getConnectionType() {
    if (this.platform.is("cordova")) {
      return this.network.type;
    } else {
      return true
    }
  }
}

app.module.ts

import { PeopleServiceProvider } from '../providers/people-service/people-service';
import { Network } from '@ionic-native/network/ngx';
import { NetworkService } from '../providers/network/network';
@NgModule({
  declarations: [
    MyApp,
    HomePage,
    LoginPage,
    IzvjestajPage,
    NalogPage,
    OtkazPage,
    ServicesPage,
    RegistrationPage
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp),

  ],

  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    LoginPage


  ],
  providers: [
    StatusBar,
    HttpClientModule,
    Geolocation,
    NativeGeocoder,
    GoogleMaps,
    StatusBar,
    LocationAccuracy,
    NetworkService,

    {provide: ErrorHandler, useClass: IonicErrorHandler},
    PeopleServiceProvider,
    Network

  ]
})
export class AppModule {}

app.component.ts

import { Component } from '@angular/core';
import { Platform, ModalController, Events } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { Network } from '@ionic-native/network/ngx';
import { ToastController } from 'ionic-angular';
import { NetworkService } from '../providers/network/network';

@Component({
  templateUrl: 'app.html'
})

export class MyApp {
  rootPage: any = LoginPage;

  infoConnection$ = this.networkService.infoConnection$;

  constructor(platform: Platform, statusBar: StatusBar, modalCtrl: ModalController, private network: Network, private toast: ToastController, private networkService: NetworkService) {
    platform.ready().then(() => {

      this.networkService.infoConnection$.subscribe(infoConnection => {
        console.log(infoConnection)
      })

    })

}

  }

0 个答案:

没有答案