前面已經介紹了Html5+ +vue 的組合方式,如果不清楚的可以翻看之前的文章。本文中使用的 this.$plusReady 函數即為封裝好的html5plus的plusReady事件。
解決方案##
this.$plusReady(()=>{
var first = null
var webview = plus.webview.currentWebview(); //獲取當前頁面的webview對象
plus.key.addEventListener('backbutton', function() {
webview.canBack(e=>{ // canBack函數用於查詢Webview窗口是否可后退
if(e.canBack){ //判斷是否可以后退
webview.back()// 調用當前webview的后退
}
else{// else代碼塊表示不能后退,也就意味着回退到首頁了
if (!first) { //連按兩次退出程序的功能實現
first = new Date().getTime()
plus.nativeUI.toast('再按一次退出應用');
setTimeout(function() {
first = null
}, 1000)
} else {
if (new Date().getTime() - first < 1000) { //這里的1000是指兩次按鍵的時間間隔在1秒內就退出應用
plus.runtime.quit() //退出應用
}
}
}
})
}, false);
});