我有一個提供程序必須在應用程序運行時始終保持運行狀態,以監視網絡連接狀態。Ionic 3提供程序的全局實例
所以按照該tutorial我已經添加了類我app.module.ts
文件,使之成爲全局實例。所以據我瞭解,當應用程序初始化它的根組件時服務應該啓動(因此app.module.ts
)。
問題:在應用程序的特定頁面導入並使用它之前,不會調用提供程序。
在提到教程provider
導入這樣的:
ionicBootstrap(MyApp, [TestProvider]);
不幸的是,它不適合我的工作。那post說這個比較新的教程已經過時了。
問題:我怎麼能使用providers
在Ionic 3
,他們是作爲一個實例啓動應用程序後?
我app.module.ts:
import { NetworkConnectionProvider } from '../providers/networkconnection/networkconnection';
// (...)
@NgModule({
declarations: [
MyApp,
// (...)
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp),
ionicGalleryModal.GalleryModalModule,
],
bootstrap: [
IonicApp
],
entryComponents: [
MyApp,
// (...)
],
providers: [
// (...)
NetworkConnectionProvider
]
})
export class AppModule {}
我的供應商:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Network } from '@ionic-native/network';
@Injectable()
export class NetworkConnectionProvider {
private TAG = "NetworkConnectionProvider ";
private isConnectedToInternet: Boolean;
constructor(
public http: Http,
public network: Network
) {
this.isConnectedToInternet = true;
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log(this.TAG + 'network was disconnected.');
this.isConnectedToInternet = false;
});
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
this.isConnectedToInternet = true;
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log(this.TAG + 'wifi connection available');
}
}, 3000);
});
console.log('Hello NetworkConnectionProvider');
}
public subscribeOnConnect() {
return this.network.onConnect();
}
public isConnected(): Boolean{
return this.isConnectedToInternet;
}
public getConnectionType(): string {
return this.network.type;
}
}