vue 路由鉴权 路由根据接口返回的数据判断是否有权限访问

vue yekong
import { Message } from 'element-ui'
const router = new Router({
  base: '/',
  routes: [
    {
      path: '/',
      name: '',
      component: Home,
      meta: {
        requiresAuth: true,
        showPage: false
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  var data = JSON.parse(localStorage.getItem('permissions'))
  var ispermissions = false
  console.log(to.path)
  if (!localStorage.getItem('token')) {
    if (to.path != '/login') {
      next('/login')
    } else {
      next()
    }
  } else {
    if (to.meta.requiresAuth) {
      data.forEach((type) => {
        if (to.path == type) {
          ispermissions = true
        }
      })
      console.log(ispermissions)
      if (ispermissions) {
        next()
      } else {
        next(from.path)
        Message({
          message: '当前用户没有查看权限',
          type: 'error',
          duration: 2 * 1000
        })
      }
      // next()
    }
  }
})
喜欢