安装物理返回按键插件:
cordova plugin add cordova-plugin-backbutton
index.html中添加:
<div id='exit' style="display: none;"></div>
在main.js中修改添加如下内容,监听物理返回按键:
document.addEventListener('deviceready', function () {
let vueInstance = new Vue(
{
el: '#app',
router,
store,
components: {App},
template: '<App/>'
});
let time;
//监听手机返回按键
document.addEventListener("backbutton", function () {
let intervalTime = new Date() - time;
let x = document.getElementById('exit');
if (x) {
//两次按键间隔时间小于一秒退出APP
if (intervalTime < 1000) {
navigator.app.exitApp();
} else {
//刷新time
time = new Date();
AnyToast.showToast("双击退出应用");
}
} else {
if(vueInstance.$route.name.toString()==="LoginPage"){
navigator.app.exitApp();
}else if(vueInstance.$route.name.toString()==="1"){
vueInstance.$router.push('/MainPage');
}else if(vueInstance.$route.name.toString()==="2"){
vueInstance.$router.push('/1');
}else if(vueInstance.$route.name.toString()==="3"){
vueInstance.$router.push('/2');
}else if(vueInstance.$route.name.toString()==="4"){
vueInstance.$router.push('/3');
}else if(vueInstance.$route.name.toString()==="5"){
vueInstance.$router.push('/4');
}else if(vueInstance.$route.name.toString()==="6"){
vueInstance.$router.push('/5');
}else if(vueInstance.$route.name.toString()==="7"){
vueInstance.$router.push('/6');
}else {
//console.log("this.$route.path.toString():",vueInstance.$route.path.toString());
//console.log("this.$route.name.toString();:",vueInstance.$route.name.toString());
//在其他页面时返回上一页面
vueInstance.$router.back(-1);
}
}
}, false);
}, false);