跳转路由,在离开页面前,需要弹框询问用户是否确定离开。
组件守卫实现操作,不需要手动操作history,注意阻止后退 next(false) 就好,
Vue Router组件内的守卫
beforeRouteLeave(to, from, next) {
// 导航离开该组件的对应路由时调用
// 这里可以if用to.path之类的判断是否拦截,记得else放行
setTimeout(() => {
vant.Dialog.confirm({
title: '提示',
message: '信息尚未提交,是否确定退出?',
})
.then(() => {
next();
})
.catch(() => {
next(false)
})
.finally(() => {
vant.Dialog.close();
})
})
}