Android平台5+ API提前生效,支持在plusready事件前调用
ios上plus是一直存在的,不涉及等ready事件。但安卓上还是需要等plus ready。
在安卓环境中,通常情况下需要html页面解析完成后才会让5+ API生效,安卓的执行的顺序为:
加载html页面,loading
解析html页面(解析title节点、下载script/link等节点引用的资源,如js/css文件)
触发DOMContentLoaded事件
触发plusready事件
此文对执行顺序有详细描述:提升HTML5的性能体验系列之五 webview启动速度优化及事件顺序解析 - DCloud问答
// 这里可以调用5+ API了,为了更好的兼容性,应该使用以下代码进行判断
// 这里可以调用5+ API了,为了更好的兼容性,应该使用以下代码进行判断
// 在这里调用5+ API
if(window.plus){
}else{// 兼容老版本的plusready事件
document.addEventListener('plusready',function () {
// 在这里调用5+ API
},false);
}
举例:
mounted () {
setTimeout(() => {
// 获取设备唯一标识码 浏览器运行时注释掉plus部分【浏览器中无plus会报错阻滞代码向下执行】
// 这里可以调用5+ API了,为了更好的兼容性,应该使用以下代码进行判断
if (window.plus) {
// 在这里调用5+ API
this.plusready()
} else { // 兼容老版本的plusready事件
document.addEventListener('plusready', this.plusready(), false)
}
})
},
method: {
plusready () {
// 在这里调用plus api 获取设备info
plus.device.getInfo({
success: function (e) {
console.info('getDeviceInfo success: ' + JSON.stringify(e))
},
fail: function (e) {
console.error('getDeviceInfo failed: ' + JSON.stringify(e))
}
})
// 获取软件版本号
plus.runtime.getProperty(plus.runtime.appid, (inf) => {
console.info('当前版本号:' + inf.version)
})
}
}