vue路由守卫应用场景(vue3.0路由守卫)

vue路由守卫应用场景(vue3.0路由守卫)全局前置守卫 beforeEach beforeResolv afterEach 使用场景 1 用于登录验证 及用户长时间不登录的时候 跳出是否满意 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 3 修改 title 注意 1 中也可以修改 但是万一不跳到下一个页面 在 1 中修改会出错 nbsp 路由独享守卫 beforeEnter nbsp nbsp nbsp nbsp nbsp



全局前置守卫:beforeEach、beforeResolve、afterEach

使用场景:1.用于登录验证(及用户长时间不登录的时候,跳出是否满意)

3.修改title,注意1中也可以修改,但是万一不跳到下一个页面,在1中修改会出错

路由独享守卫:beforeEnter

用于需要特别处理的情况(针对beforeEach)

const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})

  

组件内的守卫:beforeRouteEnter、beforeRouteUpdadte、beforeRouteLeave

注意:1.此时的this是

2.可以通过this访问实例

3.跳转前判断

常用来禁止用户在还未保存修改前突然离开,该导航可以通过next( false )来取消。

  

beforeRouteLeave (to, from , next) {
const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
if (answer) {
next()
} else {
next(false)
}
}

编程小号
上一篇 2025-02-11 15:33
下一篇 2025-03-02 14:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/24819.html