import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const routes = [
{
path: '/',
name: "欢迎",
meta: {
requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的
},
component: resolve => require(["../components/Hello.vue"], resolve)
},
{
path: '/login',
name: "登录",
/*meta: {
requireAuth: false, // 添加该字段,表示进入这个路由是需要登录的
},*/
component: resolve => require(["../view/login/login.vue"], resolve)
}
]
export default new Router({
base: "/",
routes
})
router.beforeEach((to, from, next) => {
if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
if (store.state.token) { // 通过vuex state获取当前的token是否存在
next();
}
else {
next({
path: '/login',
query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由
})
}
}
else {
next();
}
})
<script>
export default {
methods:{
login(){
this.$store.commit('setToken','true'); //改变token状态
let redirect = decodeURIComponent(this.$route.query.redirect || '/'); 获取登录成功后要跳转的路由。
this.$router.push({
path: redirect
})
}
}
}
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有