vue3-多级路由实现登录页面与业务功能页面分离

vue3所开发的通常是单页面的应用,通常的做法是先创建一个xxx.vue作为主体框架,以一个 admin风格的页面为例,通常包括顶部、左侧边栏、中间核心内容区域等,具体可参考ElementPlus页面的例子( Container 布局容器 | Element Plus (gitee.io))。通过点击左侧边栏,在中间区域显示具体某个页面(在vue中其实是组件)的内容。

但是,对于像登录页、404页等页面,如果也以此上述vue文件为框架,则相当于登录页面左侧也同样会显示侧边栏,这不符合要求。

因此,在本项目中,通过使用多级路由的方式,实现登录页面与业务功能页面相互分离。

App.vue

app.vue仅需一个空白的页面即可。

<template>
  <div>
    <RouterView />
  </div>
</template>

MainView.vue

一个简单的样例如下:

<template>
  
  <el-container class="layout-container-demo" >
    <el-aside width="200px" class="aside">
      <el-scrollbar>
        <h2>PROJECT</h2>
        <el-menu :default-openeds="['1']" :router="true">

          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-sub-menu index="1">
            <template #title>
              <el-icon><Monitor /></el-icon>主机管理
            </template>
            <el-menu-item-group>
              <template #title>OpenStack</template>
              <el-menu-item index="/host/openstack/aaa">创建虚机</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group>
              <template #title>Linux</template>
              <el-menu-item index="/host/linux/bbb">系统设置</el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header>
        <div class="toolbar">
          <el-dropdown>
            <el-icon style="margin-right: 8px; margin-top: 1px"
              ><setting
            /></el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item><el-button link @click="userLogout">LogOut</el-button></el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <span> {{ username }} </span>
        </div>
      </el-header>

      <el-main class="middle">
          <RouterView />
      </el-main>
    </el-container>
  </el-container>

</template>

Login.vue

<template>
  <div>
    <div style="display: flex;justify-content: center;margin-top: 150px">
      <el-card style="width: 500px">
        <div class="clearfix" style="text-align:center">
          <h2>用户登录</h2>
        </div>
        <table style="margin-top:40px;width:100%">
          <tr>
            <td>用户</td>
            <td>
                <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
            </td>
          </tr>
          <tr>
            <td>密码</td>
            <td>
                <el-input type="password" v-model="user.password" placeholder="请输入密码" @keydown.enter="doLogin"></el-input>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <el-button style="width: 50%;margin-left:25%;margin-top:20px" type="primary" @click="doLogin">登录</el-button>
            </td>
          </tr>
        </table>
      </el-card>
    </div>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';
import router from '@/router/index.js'
import store from '@/stores/index.js'

  export default {
    data() {
      return { 
        user: {
          username:'',
          password:''
        }
      }
    },
    methods: {
        doLogin() {
        }
    }
  }
</script>

router/index.js

关键点来了,看代码

import MainView from '../views/MainView.vue'
import LoginView from '../views/Login.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: MainView
    },
    {
      path: '/host',
      name: 'Host',
      component: () => import('../views/MainView.vue'),
      children:[
        {
          path: 'openstack/aaa',
          name: 'aaaaa',
          component: () => import('@/components/OpenstackAaaaaa.vue'),
          meta:{
          }
        },
        {
            path: 'linux/bbb',
            name: 'xxxxxx',
            component: () => import('@/components/LinuxBbbbbb.vue'),
            meta:{
            }
        }
        
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta:{title:"登录"}
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NoFound',
        component: () => import('@/views/404.vue'),
        meta:{title:"404"}
    },
  ]
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-element-admin 是基于 Vue.js 和 Element UI 实现的后台管理系统框架。它采用了 Vue Router 实现路由跳转和页面切换。在 Vue-element-admin 中,实现多级路由缓存可以通过以下步骤: 1. 在路由配置文件中,设置需要缓存的路由组件的 meta 属性中添加 keepAlive: true。 ``` const routes = [ { path: '/', component: Layout, redirect: '/dashboard', children: [ { path: 'dashboard', component: () => import('@/views/dashboard/index'), name: 'Dashboard', meta: { title: 'Dashboard', icon: 'dashboard', keepAlive: true } } ] } ] ``` 2. 在 App.vue 文件中,添加 keep-alive 组件,并设置 include 属性为需要缓存的路由组件的名称。 ``` <template> <div id="app"> <keep-alive :include="cachedViews"> <router-view /> </keep-alive> </div> </template> <script> export default { name: 'App', computed: { cachedViews() { return this.$store.state.tagsView.cachedViews } } } </script> ``` 3. 在 store 文件夹中创建 tagsView.js 文件,设置需要缓存的路由组件的名称和缓存状态。 ``` const state = { cachedViews: [] } const mutations = { ADD_CACHED_VIEW: (state, view) => { if (state.cachedViews.includes(view.name)) return if (!view.meta.keepAlive) return state.cachedViews.push(view.name) }, DEL_CACHED_VIEW: (state, view) => { const index = state.cachedViews.indexOf(view.name) if (index > -1) { state.cachedViews.splice(index, 1) } } } const actions = { addCachedView({ commit }, view) { commit('ADD_CACHED_VIEW', view) }, delCachedView({ commit }, view) { commit('DEL_CACHED_VIEW', view) } } export default { namespaced: true, state, mutations, actions } ``` 这样就可以实现多级路由缓存了。需要注意的是,路由组件的名称必须是唯一的,否则可能会出现缓存错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值