Лучший способ сделать это, сохраняя все ваши глобальные переменные из cordova в сервисе, который является общим для root.
В вашем app.component
или вашем корневом компоненте объявите эти переменные:
//Use "declare" to have variables that store cordova and pluggins before Component declaration
declare var StatusBar: any;
declare var cordova: any;
declare var window:any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
В конструкторе компонента введите сервис, который будет хранить детали:
//In constructor you inject a service (you can call it cordovaService)
cosntructor(private cordovaService:CordovaService){}
Вы должны добавить ngAfterViewInit
и добавить document.addEventListener('device ready')
ngAfterViewInit() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
}
В onDeviceReady
вы можете обрабатывать и хранить связанные с Cordova вещи:
onDeviceReady() {
this.cordovaService.cordova=cordova;
cordova.getAppVersion.getVersionNumber().then(appVersion => {
console.log(appVersion);
});
}
Теперь любой компонент, который внедряет CordovaService
, сможет получить доступ и использовать эти данные / переменную.