网络提供商出了什么问题?

时间:2019-03-04 08:34:37

标签: angular typescript ionic-framework dependency-injection

我在下面遇到此错误。另外,当我没有错误时,我的网络提供商也无法正常工作。也许有人知道如何解决这个问题,这是我很多天之前的事情。我已经尝试了youtube视频中的一些操作,但仍然无法正常工作。有人说这是离子4的错误

Uncaught Error: Can't resolve all parameters for NetworkService: (?, ?, ?).
        at syntaxError (compiler.js:486)
        at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15706)
        at CompileMetadataResolver._getTypeMetadata (compiler.js:15541)
        at CompileMetadataResolver._getInjectableMetadata (compiler.js:15521)
        at CompileMetadataResolver.getProviderMetadata (compiler.js:15881)
        at compiler.js:15792
        at Array.forEach (<anonymous>)
        at CompileMetadataResolver._getProvidersMetadata (compiler.js:15752)
        at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15320)
        at JitCompiler._loadModules (compiler.js:34413)

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 { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { Geolocation } from '@ionic-native/geolocation';
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderForwardResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder';
import { GoogleMap, GoogleMaps } from '@ionic-native/google-maps';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { IzvjestajPage } from '../pages/izvjestaj/izvjestaj';
import { LoginPage } from '../pages/login/login';
import { NalogPage } from '../pages/nalog/nalog';
import { OtkazPage } from '../pages/otkaz/otkaz';
import { ServicesPage } from '../pages/services/services';
import { RegistrationPage } from '../pages/registration/registration';
import { PeopleServiceProvider } from '../providers/people-service/people-service';
import { LocationAccuracy } from '@ionic-native/location-accuracy';
import { ExtendedDeviceInformation } from '@ionic-native/extended-device-information';
import { Network } from '@ionic-native/network';
import { IonicStorageModule } from '@ionic/storage';
import { NetworkService } from '../providers/network/network';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    LoginPage,
    IzvjestajPage,
    NalogPage,
    OtkazPage,
    ServicesPage,
    RegistrationPage


  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()



  ],

  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    LoginPage,
    IzvjestajPage,
    NalogPage,
    OtkazPage,
    ServicesPage,
    RegistrationPage

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



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


  ]
})
export class AppModule {}

network.ts

import { Injectable } from '@angular/core';
import { Network } from '@ionic-native/network';
import { BehaviorSubject, Observable } from 'rxjs';
import { ToastController, Platform } from 'ionic-angular';

export enum ConnectionStatus {
  Online,
  Offline
}

//@Injectable({
//  providedIn: 'root'
//})
export class NetworkService {

  private status: BehaviorSubject<ConnectionStatus> = new BehaviorSubject(ConnectionStatus.Offline);

  constructor(private network: Network, private toastController: ToastController, private plt: Platform) {
    this.plt.ready().then(() => {
      this.initializeNetworkEvents();
      let status = this.network.type !== 'none' ? ConnectionStatus.Online : ConnectionStatus.Offline;
      this.status.next(status);
    });
  }

  public initializeNetworkEvents() {

    this.network.onDisconnect().subscribe(() => {
      if (this.status.getValue() === ConnectionStatus.Online) {
        console.log('WE ARE OFFLINE');
        this.updateNetworkStatus(ConnectionStatus.Offline);
      }
    });

    this.network.onConnect().subscribe(() => {
      if (this.status.getValue() === ConnectionStatus.Offline) {
        console.log('WE ARE ONLINE');
        this.updateNetworkStatus(ConnectionStatus.Online);
      }
    });
  }

  private async updateNetworkStatus(status: ConnectionStatus) {
    this.status.next(status);

    let connection = status == ConnectionStatus.Offline ? 'Offline' : 'Online';
    console.log('test');
    let toast = this.toastController.create({
      message: `You are now ${connection}`,
      duration: 3000,
      position: 'bottom'
    }).present();
  };

  public onNetworkChange(): Observable<ConnectionStatus> {
    return this.status.asObservable();
  }

  public getCurrentNetworkStatus(): ConnectionStatus {
    return this.status.getValue();
  }
}

1 个答案:

答案 0 :(得分:0)

该错误消息基本上是说Angular的依赖注入无法确定构造函数(NetworkService)想要的对象。因此,您可以从构造函数中删除参数(如果不需要),或者需要确保所有对象都已在DI中注册。

您的NetworkService唯一的构造函数具有多个参数,但是您没有指定应该如何注入app.module……因此我猜这些参数的类型之一是错误的。例如ToastController中没有明显的app.module

或者,您可能只需要NetworkService中的无参数构造函数。

特别是对于您的NetworkService代码,构造函数比构建对象要做的更多,我会考虑只保留let status...行并将该平台内容移至单独的init函数