vue-router控制路由权限

796 阅读1分钟
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const router = new VueRouter({ routes: [
  { path: '/' },
  { path: '/login' },
  { path: '/goods', meta: { requiresAuth: true } },
  { path: '/customers', meta: { requiresAuth: true } },
] })

router.beforeEach((to, from, next) => {
  // 可以在路由元信息指定哪些页面需要登录权限
  const islogin = false // 假设没有登录(这里应从接口获取)
  if (to.meta.requiresAuth && !islogin) { // 需要登录授权,这里还需要判断是否登录
    next('/login') // 跳转到登录
    return
  }
  next()
}){ path: '/goods', meta: { requiresAuth: true } },