나는 네트워크 연결 상태를 감시하기 위해 앱을 실행할 때 항상 작동해야하는 공급자가 있습니다.Ionic 3에있는 공급자의 글로벌 인스턴스
따라서 tutorial에 따르면 app.module.ts
파일에 클래스를 추가하여 글로벌 인스턴스으로 만들었습니다. 그래서 내가 이해하는 한, 응용 프로그램이 루트 구성 요소 (따라서 app.module.ts
) 초기화 할 때 서비스가 있어야합니다.
문제 : 응용 프로그램의 특정 페이지가 가져 와서 사용할 때까지 공급자가 호출되지 않습니다. 언급 자습서 provider
에서
ionicBootstrap(MyApp, [TestProvider]);
이 불행하게도 나를 위해 작동하지 않습니다. 그 post 꽤 새로운 튜토리얼은 구식입니다 말합니다.
질문 : 어떻게 그들이 응용 프로그램을 실행 한 후 하나의 인스턴스로 사용할 수 있습니다 Ionic 3
에서 providers
사용할 수 있을까?
내 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;
}
}