如题,这种时候监听路由会十分不便,并且还要做到拦截,使用onBeforeRouteLeave即可。
import { onBeforeRouteLeave } from "vue-router"; // 需要v3引入
onBeforeRouteLeave((to, from, next) => {
ElMessageBox.confirm("修改尚未保存,是否确认离开?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(() => {
next()
}).catch(() => { });
}
});