在pages.json配置
{
"path": "pages/confirmPost/confirmPost",
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false,
"navigationStyle": "custom"
}
}
onShow中添加监听,onUnload和onHide中取消监听
<template>
<view>
</view>
</template>
<script>
export default {
data() {
return {
};
},
onShow() {
var state = {
title: 'title',
url: '#',
};
history.pushState(state, 'title', document.URL);
window.addEventListener('popstate', this.handleback);
},
methods: {
/**
* @description 阻止用户返回事件
* */
handleback() {
var vm = this;
var myback = function() {
var state = {
title: 'title',
url: '#',
};
history.pushState(state, 'title', document.URL);
}.bind(vm);
myback;
},
},
onUnload() {
setTimeout(() => {
window.removeEventListener('popstate', this.handleback);
}, 300);
window.clearInterval(this.timer);
this.timer = null;
},
onHide() {
window.removeEventListener('popstate', this.handleback);
window.clearInterval(this.timer);
this.timer = null;
},
};
</script>
<style lang="scss" scoped>
</style>