HBuilder打包vue項目時,發現app按返回鍵就直接退出應用了,用了大佬提供的方法,鏈接如下:
https://blog.csdn.net/qq_25252769/article/details/76913083
我用的是需要mui.js版本的代碼,再模擬運行的時候可以按手機的返回鍵返回上一級,但是不能按兩次後退出,提示
TypeError: Cannot read property 'canBack' of undefined
後來看文檔發現如果引入mui.js的話,就需要重寫back方法,因爲muil.back()裏面綁定了Android的物理back鍵。
重寫了back方法,如下:
// eslint-disable-next-line no-undef
mui.init({
// 關閉事件監聽
keyEventBind: {
backbutton: true
}
})
var first = null
// eslint-disable-next-line no-undef
mui.back = function () {
if (!first) {
first = new Date().getTime()
// eslint-disable-next-line no-undef
mui.toast('再按一次退出應用')
history.go(-1)
setTimeout(function () {
first = null
}, 1000)
} else {
if (new Date().getTime() - first < 1500) {
// eslint-disable-next-line no-undef
plus.runtime.quit()
}
}
}