vue3 + TS + elementplus + pinia实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容

这篇具有很好参考价值的文章主要介绍了vue3 + TS + elementplus + pinia实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

效果图:

vue3 + TS + elementplus + pinia实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容

 home.vue页面代码

<template>
  <el-container>
    <el-aside width="collapse ? 200px : 70px">
      <el-button color="#626aef" @click="collapseToggle()">
        <el-icon>
          <Expand v-if="collapse" />
          <Fold v-else />
        </el-icon>
      </el-button>
      <el-menu
          :collapse="collapse"
          :default-active="store.bbc"
          class="el-menu-vertical-demo"
          unique-opened
          active-text-color="#ffd04b"
          text-color="#fff"
          background-color="transparent"
          @select="store.vv"
         >
       <left :dataToSon="store.mm" />
      </el-menu>
    </el-aside>
    <el-container>
      <el-header height="80px">
        <h1 @click="fff">大可的管理系统 - v1.0</h1>
        <div>
          <img src="@/assets/111.jpg" alt="">
          <span></span>
          <el-button type="primary" @click="LogOut">退出登录</el-button>
        </div>
      </el-header>
      <el-main>
        <tab></tab>
      </el-main>
      <el-footer height="50px">
        <p>&copy; 版权所有: 大可</p>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from "vue-router";
import left from "../left.vue";
import  tab from '../tab.vue';
import { ElMessage, ElMessageBox} from "element-plus";
import { useAuthStore } from '@/store';
import preventBack from 'vue-prevent-browser-back';//组件内单独引入
const mixins = [preventBack];
const store = useAuthStore();
const collapse = ref<boolean>(false)
const router = useRouter();
const tiao = () => {
  console.log('路由')
  router.push('/son1')
}
const fff = () => {
  router.replace('/son2')
}
const collapseToggle = () => {
  collapse.value = !collapse.value
}
const ggvv  = ref([1,2,3])
const handleOpen = () => {
  console.log()
}
const gg = (e) => {
  console.log(e)
}
const handleClose = () => {
  console.log()
}
const LogOut = () => {
  ElMessageBox.confirm(
      '确定要退出登录?',
      'Warning',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        router.replace('/login')
        ElMessage({
          type: 'success',
          message: '退出成功',
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '您取消了退出',
        })
      })
}
</script>

<style scoped>
.el-header {
  background: url("@/assets/111.jpg");
  background-color: #f3d19e;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-header h1 {
  font-size: 26px;
  color: #fff;
  letter-spacing: 10px;
}
.el-header div {
  margin-right: 30px;
}
.el-header img {
  width: 40px;
  border-radius: 40px;
  vertical-align: middle;
  margin-right: 10px;
}
.el-header span {
  font-size: 18px;
  color: #fff;
  margin-right: 10px;
}
.el-header el-button {
  margin-left: 10px;
}

.el-aside {
  height: 100vh;
  background: url('@/assets/111.jpg');
  transition: width 0.3s;
  text-align: right;
}
.el-aside .el-button {
   margin: 20px 10px 20px 0;
 }
.el-aside .el-menu {
  border-right: none;
}
.el-footer {
  background-color: #EBEEF5;
  display: flex;
  align-items: center;
}
.el-footer p {
  font-size: 12px;
  color: #666;
}


</style>

left.vue页面代码

<script setup lang="ts">
import { ref, defineProps } from 'vue';
import { useAuthStore } from '@/store';
type HeaderProps = {
  dataToSon: number[];
};
// 2. type 声明的HeaderProps 用 defineProps注册
const zz = defineProps<HeaderProps>();
const hhkk = zz.dataToSon;
const list = ref<Array<object>>([]);
const store = useAuthStore();
const gg = () => {

}
</script>

<template>
    <template v-for="(item, index) in hhkk" :key="item.id">
      <!-- 非叶子节点 -->
      <el-sub-menu v-if="item.children" :index="item.id">
        <template #title>
          <el-icon>
            <Setting />
          </el-icon>
          <span v-text="item.name"></span>
        </template>
        <left :dataToSon="item.children"/>
      </el-sub-menu>
      <!-- 叶子节点(功能节点) -->
      <el-menu-item v-else :index="item.path">
        <el-icon>
          <Menu />
        </el-icon>
        <span v-text="item.name"></span>
      </el-menu-item>

    </template>
</template>

<style scoped lang="stylus">
</style>

tab.vue页面代码

<template>
  <el-tabs
      v-model="store.bbc"
      type="card"
      class="demo-tabs"
      closable
      @tab-remove="store.kk"
      @tab-click="jj"
  >
<!--    @tab-click="jj"-->
    <el-tab-pane
        v-for="item in ff"
        :key="item.id"
        :label="item.name"
        :name="item.path"
    >
      <router-view :name="item.path" v-slot="{Component}">
        <keep-alive>
          <component :is="Component"></component>
        </keep-alive>
      </router-view>
    </el-tab-pane>
  </el-tabs>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {useAuthStore} from '@/store';
import {useRouter} from "vue-router";
const store = useAuthStore();
const router = useRouter();
onMounted(() => {
  // store.bbc = router.currentRoute._value.fullPath;
})

const fv = store.bbc;

const jj = (e) => {
  // router.replace(e.props.name)
  console.log(e)
}
const hh = (ee) => {
  console.log(ee)
}
let tabIndex = 2
const editableTabsValue = ref(store.zx.length)
const ff = store.zx;
const gg = (e) => {
  ff.forEach(item => {
    if (item.path == e) {
      ff.splice(item,1)
    }
  })
}
</script>

<style scoped>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>

pinia里面的代码

import { defineStore } from 'pinia';

export default defineStore('auth',  {
   state: () => {
       return {
           loginName: "张三",
           ss: [],
           mm: [
               {
                 path: 'vv',
                 name:'首页',
                 func_fid: 0,
                 id:"1000",
                 children: [
                     {
                         path: 'sy',
                         name:'首页儿子',
                         func_fid: 1000,
                         id:"1212",
                     }
                 ]
               },
               {
                   path:'hh',
                   name:'系统管理',
                   func_fid: 0,
                   id: '1',
                   children: [
                       {
                           id: '1',
                           func_fid: 1,
                           path:'son1',
                           name: '系统管理儿子',
                       },
                       {
                           id:' 2',
                           func_fid: 1,
                           path:'hhh',
                           name: '系统管理-角色',
                           children: [
                               {
                                   id: '222',
                                   func_fid: 1,
                                   path: 'son1-1-1',
                                   name: '角色管理',
                                   children:[
                                       {
                                           id: '12',
                                           func_fid: 2,
                                           path: 'son1-1-1',
                                           name: '角色管理儿子',
                                           children:[
                                               {
                                                   id:' 122',
                                                   func_fid: 2,
                                                   path: 'son1-1-1',
                                                   name: '角色管理儿子-----孙子',
                                               }
                                           ]
                                       }
                                   ]
                               }
                           ]
                       },
                       {
                           id: '12',
                           path: 'son1-2',
                           name: '用户管理'
                       }

                   ]
               },
               {
                   path:'ss',
                   name:'教学管理',
                   id: '22',
                   func_fid: 0,
                   children: [
                       {
                           path:'son2',
                           name:'教学管理儿子',
                           id: '202',
                           func_fid: 22,
                       }
                   ]
               },
               {
                   path:'zz',
                   name:'行政管理',
                   id: '3',
                   func_fid: 0,
                   children: [
                       {
                           path:'son3',
                           name:'行政管理儿子',
                           id: '33',
                           func_fid: 3,
                       }
                   ]
               },

           ],
           zx:[],
           bbx:[],
           bbc: '',
       }
   },
    persist: {
        enabled: true, // 开启缓存  默认会存储在本地localstorage
        storage: sessionStorage, // 缓存使用方式
        paths:[] // 需要缓存键
    },
    actions: {
       zz(ss:string) {
           this.loginName = ss
       },
       vv(e) {
           let hh = [];
           function traverse(node) {
               hh.push(JSON.parse(JSON.stringify(node)))
               if (node.children && node.children.length > 0) {
                   for (let i = 0; i < node.children.length; i++) {
                       traverse(node.children[i]);
                   }
               }
           }
           this.mm.forEach(item => {
               traverse(item)
           })
           const bb =[]
           hh.forEach(item => {
              if (item.path == e) {
                  const index = this.zx.findIndex(i => JSON.stringify(i) === JSON.stringify(item));
                  this.bbc = item.path;
                  if (index === -1) {
                      this.zx.push({...item})
                  }
              }
           })
       },
        kk(e) {
            let i = this.zx.findIndex(item => item.path === e);
            if(e != this.bbc) {} //删除的是一个不激活的tab那么就什么都不做
            else if (this.zx.length === 1) //删除的是最后剩下的一个激活的tab
                this.bbc = '';
            else if (i === this.zx.length - 1) //删除的是最末尾的一个tab,让前面那个激活
                this.bbc = this.zx[i - 1].path;
            else                                 //删除的是中间的一个激活的tab
                this.bbc = this.zx[i + 1].path;

            this.zx.splice(i, 1);
        },
    }
})

安装

yarn add pinia-plugin-persistedstate
or
npm i  pinia-plugin-persistedstate

使用插件 在main.ts中注册

import { createApp } from "vue";
import App from "./App.vue";
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);
createApp(App).use(pinia);

路由代码

// import {createRouter, createWebHistory, createWebHashHistory} from "vue-router";

// // 1. 配置路由
// const routes: ({ redirect: string; path: string } | { path: string; component: () => Promise<any> } | { path: string; component: () => Promise<any>; children: ({ path: string; components: { son2: () => Promise<any> } } | { path: string; components: { son3: () => Promise<any> } })[] } | { path: string; components: { son1: () => Promise<any> }; name: string; props: { son1: boolean } })[] = [
//     {path: '/', redirect: '/login'},
//     {path: '/login', component: () => import('../views/login.vue')},
//     {
//         path: '/home',
//         component: () => import('../views/home/home.vue'),
//         children: [
//             // {path: 'son1', name: 'son1', components: {son1: () => import('../views/home/son/son1.vue')}},
//             {path: 'son2', components: {son2: () => import('../views/home/son/son2.vue')}},
//             {path: 'son3', components: {son3: () => import('../views/home/son/son3.vue')}},
//         ]
//     },
//     {
//         path: '/son1',
//         name: 'son1',
//         components: {son1: () => import('../views/home/son/son1.vue')},
//         props: { son1: true }
//     }
//
// ];
// // 2.返回一个 router 实列,为函数,配置 history 模式
// const router = createRouter({
//     history: createWebHashHistory(),
//     routes,
// });
//
//
// // 3.导出路由   去 main.ts 注册 router.ts
//
// export default router

import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(),
    routes: [
            {path: '/', redirect: '/login'},
    {path: '/login', component: () => import('../views/login.vue')},
    {
        path: '/home',
        component: () => import('../views/home/home.vue'),
        children: [
            {
                path:'',
                components: {
                    son1: () => import('../views/home/son/son1.vue'),
                    son2: () => import('../views/home/son/son2.vue'),
                    son3: () => import('../views/home/son/son3.vue'),
                    sy:() => import('../views/home/son/sy.vue'),
                }
            },
        ]
    },
    ]
})

export default router

我把代码放git上了,有需要的自行拉取

https://gitee.com/Flechazo7/vue3.git文章来源地址https://www.toymoban.com/news/detail-492515.html

到了这里,关于vue3 + TS + elementplus + pinia实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 从零开始Vue3+Element Plus后台管理系统(六)——状态管理Pinia和持久化

    官网:https://pinia.vuejs.org/zh/ Pinia 是 Vue 的专属状态管理库,相比Vuex更好用,优点不多了说官网有,用起来最重要! 在应用的根部注入创建的 pinia 定义store,拿用户登录举个简单例子 在src目录新建文件夹store,然后新建文件user.ts 在Vue页面中使用Pinia 如果我们把登录用户的信息

    2024年02月05日
    浏览(33)
  • Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    手动安装配置Vue3 ElementPlus模板比较繁琐,网上寻找一些模板不太符合自己预期,因此花点精力搭建一个符合自己需求的架子 采用最新的组件,版本如下: vite 4.3.9 vite-plugin-mock 2.9.8 vue 3.3.4 pinia 2.1.3 vue-router 4.2.2 element-plus 2.3.6 满足自己以下功能: Vite工具热启动速度快,修改后

    2024年02月08日
    浏览(36)
  • 前端开发小技巧 - 【Vue3 + TS】 - 在 TS + Vue3 中使用 Pinia,实现 Pinia 的持久化,优化Pinia(仓库统一管理)

    ts 中使用 pinia 和 Vue3 基本一致,唯一的不同点在于,需要根据接口文档给 state 标注类型,也要给 actions 标注类型; 以下都是 组合式API 的写法, 选项式API 的写法大家可以去官网看看; Pinia; 持久化插件 - pinia-plugin-persistedstate; 目标文件: src/types/user.d.ts (这里以 user.d.t

    2024年04月09日
    浏览(35)
  • Vue3+Vite+Pinia+Naive后台管理系统搭建之四:Naive UI 组件库的安装和使用

    前言 如果对 vue3 的语法不熟悉的,可以移步 Vue3.0 基础入门Vue3.0 基础入门快速入门。 UI 组件请参考官网:Naive Ui 官网 为什么选择 naive ui 不继续用 element ui,因为尤大大推荐,可以尝试下,而且 naive ui 更贴近 vue3 的语法,当然易上手还是element ui 好一点。 github 开源库:Vue

    2024年02月07日
    浏览(41)
  • Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后台管理系统模板(已开源---显示项目页面截图)

    Wocwin-Admin,是基于 Vue3.2、TypeScript、Vite、Pinia、Element-Plus、Qiankun(微前端) 开源的一套后台管理模板;同时集成了微前端 qiankun也可以当做一个子应用。项目中组件页面使用了Element-plus 二次封装 t-ui-plus 组件,目前已新增fastmock接口。 Link:https://wocwin.github.io/wocwin-admin/ 账号:

    2024年02月08日
    浏览(44)
  • axios实战进阶练习——基于 Vue3 + Node.js + ElementPlus 实现的联系人列表管理后台

    书接上回,上一篇文章介绍了一个基于 Vue3 和 ElementPlus 的联系人列表管理后台小 demo (Vue3 + ElementPlus实战学习——模拟简单的联系人列表管理后台),在有了上一篇文章的基础上,我们试着用 axios 来获取数据,而不是用写死的数据,然后用 Node.js + Vue3 + ElementPlus 来实现联系

    2024年02月09日
    浏览(29)
  • axios 实战进阶练习——基于 Vue3 + Node.js + ElementPlus 实现的联系人列表管理后台

    书接上回,上一篇文章介绍了一个基于 Vue3 和 ElementPlus 的联系人列表管理后台小 demo (Vue3 + ElementPlus实战学习——模拟简单的联系人列表管理后台),在有了上一篇文章的基础上,我们试着用 axios 来获取数据,而不是用写死的数据,然后用 Node.js + Vue3 + ElementPlus 来实现联系

    2024年02月10日
    浏览(35)
  • vue3 + tsrpc +mongodb 实现后台管理系统

    之前上线了一个vue后台管理系统,有小伙伴问我有没有后端代码,咱只是个小前端,这就有点为难我了。不过不能辜负小伙伴的信任,nodejs也可以啊,废话不多说,开搞!后端采用 TSRPC 框架实现 API 接口,前端采用 vue-manage-system 后台管理系统框架,数据库采用 mongodb。TSRPC 是

    2024年01月16日
    浏览(48)
  • vue3 一个基于pinia简单易懂的系统权限管理实现方案,vue-router动态路由异步问题解决

    作为项目经验稀少的vue开发者来说,在关键技术点上的经验不多,我希望通过我的思想和实践,把好的东西分享在这里,目的是进一步促进技术交流。项目即将完成,权限是最后的收尾工作,好的权限实现方案,可以让我们没有后顾之忧,也可以提升项目的运行速度。 在开发

    2023年04月08日
    浏览(33)
  • vue3后台管理系统之实现分页功能

    例子:用户 请求格式 返回数据类型 设置返回数据类型 属性 首先我们先要了解分页器的属性 v-model:current-page当前页码 v-model:page-size设置每页展示数据的条数 page-sizes每页显示个数选择器的个数 small是否使用小型分页样式 disabled 是否禁用分页 background 是否为分页按钮添加背景

    2024年02月06日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包