【问题标题】:Use cordova plugins in angular 6 application在 Angular 6 应用程序中使用 cordova 插件
【发布时间】:2018-12-06 22:38:19
【问题描述】:
【问题讨论】:
标签:
angular
cordova
cordova-plugins
【解决方案1】:
我不知道这是否是更好的选择,但我通常 - 当我可以使用 cordova 插件时 - 使用“服务”来存储值或对象。例如你的 main.app.ts 可以像
//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 called cordovaService)
cosntructor(private cordovaService:CordovaService){}
//You must add in ngAfterViewInit a document.addEventListener('device ready')
ngAfterViewInit() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
}
onDeviceReady() {
//Typical you use to defined aht happens when, pause, resume,backbutton..
document.addEventListener('pause', this.onPause.bind(this), false);
document.addEventListener('resume', this.onResume.bind(this), false);
document.addEventListener("backbutton", this.onBackKeyDown.bind(this), false);
//MoreOver you have there your "plugging"
//for example, if add StatusBar pluging you have "StatusBar"
//So you can
StatusBar.hidden();
//Or if you have a variable "StatusBar" in your service
this.cordovaService.StatusBar=StatusBar
//so, any component that inject "CordovaService" would make a
//this.cordovaService.StatusBar.hide() or this.cordovaService.StatusBar.show()
//idem if you has imported getAppVersion, you can do
cordova.getAppVersion.getVersionNumber().then(version => {
console.log(version)
});
//Or make
this.cordovaService.cordova=cordova
//so, any component that inject "CordovaService" would make a
//this.cordovaService.cordova.getAppVersion.getVersionNumber().then(version =>
// {
// console.log(version)
// });
}
如果插件“正在播放”,我想你可以采用这个“技术”