Vue.js 如何创建中间件或如何为前端和管理员管理以下路由

Vue.js 如何创建中间件或如何为前端和管理员管理以下路由,vue.js,routes,vue-component,vue-router,middleware,Vue.js,Routes,Vue Component,Vue Router,Middleware,如何通过Vue中的中间件管理前端和管理面板的url 这是我在router/index.js文件中编写的代码: const router = new VueRouter({ mode: 'history', routes }); router.beforeEach((to, from, next) => { // redirect to login page if not logged in and trying to access a restricted page con

如何通过Vue中的中间件管理前端和管理面板的url

这是我在router/index.js文件中编写的代码:

const router = new VueRouter({ mode: 'history', routes });

  router.beforeEach((to, from, next) => {

  // redirect to login page if not logged in and trying to access a restricted page
  const loggedIn = localStorage.getItem('usertoken') == null ? false : true;
  const user = JSON.parse(localStorage.getItem('user'));

  //this is for admin
  next('/admin/login')
  next('/admin/home');

  //this is my front URL
  next('/terms-condition');
  next('/home');
  next()
})
export default router;

您实施的方式是正确的

用户成功登录后,使用if-else条件重定向到管理面板,并使用vue路由器中提供的导航保护


这有助于防止其他用户直接使用此url,您的实现方式是正确的

用户成功登录后,使用if-else条件重定向到管理面板,并使用vue路由器中提供的导航保护


这有助于防止其他用户直接使用此url

请参阅下面的代码,它可能会帮助您

/** *用于身份验证的中间件 */

您需要创建两个路由器文件,一个用于前台,另一个用于管理员:

//前面的路由文件如下所示

export default [{
path: '/',
meta: { auth: false, portal: 'front' },
component: () => import('@/components/layouts/front/main.vue'),
children: [
    {
        path: '/',
        name: 'front-home',
        title: 'Dashboard',
        meta: { auth: false, portal: 'front' },
    }
   ]
}]
//管理员路由器文件将类似

export default [
 {
    path: 'user',
    name: 'users',
    title: 'Users',
    meta: { auth: true, portal: 'admin' },
    component: () => import('@/components/templates/admin/user'),
 }
]

主要区别在于定义了通过相应路径访问哪个门户的门户。如果没有meta内部的门户,它将无法工作。

请参阅下面的代码,它可能会对您有所帮助

/** *用于身份验证的中间件 */

您需要创建两个路由器文件,一个用于前台,另一个用于管理员:

//前面的路由文件如下所示

export default [{
path: '/',
meta: { auth: false, portal: 'front' },
component: () => import('@/components/layouts/front/main.vue'),
children: [
    {
        path: '/',
        name: 'front-home',
        title: 'Dashboard',
        meta: { auth: false, portal: 'front' },
    }
   ]
}]
//管理员路由器文件将类似

export default [
 {
    path: 'user',
    name: 'users',
    title: 'Users',
    meta: { auth: true, portal: 'admin' },
    component: () => import('@/components/templates/admin/user'),
 }
]
主要区别在于门户定义了通过各自的路径访问哪个门户。没有门户内部的元,它将无法工作