Skip to content

全局前置守卫

全局前置守卫的作用是对请求进行拦截,在main.js文件中对全局前置守卫进行设置

js
import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import router from './router'

const app = createApp(App)

app.use(ElementPlus)
app.use(router)

router.beforeEach((to, from, next) => {
    console.log("to:", to)  //即将进入的路由信息
    console.log("from:", from)  //当前即将离开的路由信息
    //next()  // 继续执行,如果注释掉,首页都不会显示
    
    // 设置条件拦截,阻止跳转到name为codeandcesium的页面
    if(to.name == 'codeandcesium'){
        next(false)
    }else{
        next()
    }
})

app.mount('#app')

from表示是从哪个内容页回来的;to表示当前的路由信息

Released under the MIT License.