環(huán)境搭建 參考 vite+vue3環(huán)境搭建
其次ui框架用了antdV(感覺比element更專業(yè)啊)
上面的都安裝好了后就可以開始了
0.配置路由文件
1.創(chuàng)建pinia緩存文件
import { defineStore } from 'pinia'
export const useMenuStore = defineStore({
id: 'menu',
state: () => {
return {
openMenu: [],
menuIdx: 0,
menuRoute: []
}
},
getters: {},
actions: {},
// 開啟數(shù)據(jù)緩存
persist: {
enabled: true,
strategies: [{
key: 'my_project',
storage: sessionStorage,
}]
}
})
2.路由入口
<script setup>
import { useMenuStore } from '@/stores/menu';
import { storeToRefs } from "pinia"
const menuStore = useMenuStore()
const { openMenu } = storeToRefs(menuStore)
</script>
<router-view v-slot="{ Component, route }">
<keep-alive :include="openMenu">
<component v-if="route.meta.keepAlive" :is="Component" :key="route.name" />
</keep-alive>
<component v-if="!route.meta.keepAlive" :is="Component" :key="route.name" />
</router-view>
3.簡單的寫個菜單樣式
抽出成獨(dú)立的組件
<template>
<div style="position: relative;border-radius: 5px;background-color: #fff;overflow: hidden;" :style="`${isScrollBtn ? 'padding: 0 20px 0 27px;' : 'padding: 0 37px 0 10px;'}`">
<div class="leftBtn" @click="handleLeft" v-show="isScrollBtn"><DoubleLeftOutlined /></div>
<div class="menu-tag" ref="tagBox" @scroll="menuScroll">
<a-tag :closable="v != 'Index'" v-for="(v, index) in openMenu" :bordered="false" :color="`${index == menuIdx ? '#108ee9' : 'blue'}`" @click="handleTag(v)" @close.prevent="handleClose(v, index)">{{v}}</a-tag>
</div>
<div class="rightBtn" @click="handleRight" v-show="isScrollBtn"><DoubleRightOutlined /></div>
</div>
</template>
<script setup>
import { DoubleRightOutlined, DoubleLeftOutlined } from '@ant-design/icons-vue';
import { ref, nextTick, watch } from 'vue'
import { useMenuStore } from '@/stores/menu';
import { storeToRefs } from "pinia"
import { useRouter, useRoute } from "vue-router"
const route = new useRoute()
const router = new useRouter()
const menuStore = useMenuStore()
const { menuIdx, openMenu, menuRoute } = storeToRefs(menuStore)
const tagBox = ref(null)
const isScrollBtn = ref(false)
let offsetWidth = 0
let scrollWidth = 0
let scrollLeft = 0
let isScroll = false // 是否在滾動
let scrollLenth = 300 //滾動距離
watch(() => route.name, async (now) => {
let Idx = openMenu.value.indexOf(now)
if(Idx == -1) {
openMenu.value.push(now)
menuRoute.value.push(route.path)
menuIdx.value = openMenu.value.length - 1
await nextTick()
isBeyond()
} else {
menuIdx.value = Idx
}
}, {immediate: true})
/** 監(jiān)聽容器盒子滾動事件 */
let scrollTimer;
const menuScroll = () => {
isScroll = true
if(scrollTimer) clearTimeout(scrollTimer);
scrollTimer = setTimeout(() => {
getOpton()
isScroll = false
}, 300);
}
/** 獲取菜單容器寬度等信息 */
const getOpton = () => {
offsetWidth = tagBox.value.offsetWidth
scrollWidth = tagBox.value.scrollWidth
scrollLeft = tagBox.value.scrollLeft
}
/** 計算寬度是否超出容器寬度 */
const isBeyond = () => {
getOpton()
if(scrollWidth > offsetWidth) {
isScrollBtn.value = true
tagBox.value.scrollTo(scrollWidth - offsetWidth + 47, 0)
} else {
isScrollBtn.value = false
}
}
const handleLeft = () => {
if(isScroll) return
tagBox.value.scrollTo(scrollLeft - scrollLenth, 0)
}
const handleRight = () => {
if(isScroll) return
tagBox.value.scrollTo(scrollLeft + scrollLenth, 0)
}
// 點(diǎn)擊菜單
const handleTag = (name) => {
let Idx = openMenu.value.indexOf(name)
menuIdx.value = Idx
router.push(menuRoute.value[Idx])
}
// 關(guān)閉菜單
const handleClose = async (name,index) => {
// 刪除的是高亮菜單
if(index == menuIdx.value) {
menuIdx.value -= 1
router.push(menuRoute.value[index - 1])
}
openMenu.value.splice(index, 1)
menuRoute.value.splice(index, 1)
await nextTick()
isBeyond()
}
</script>
<style lang='less' scoped>
:deep(.menu-tag) {
cursor: pointer;
user-select: none;
}
.leftBtn,.rightBtn {
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: #fff2f0;
position: absolute;
top: 0;
bottom: 0;
background-color: rgb(60 60 60 / 88%);
width: 20px;
z-index: 1;
box-shadow: #fff inset 0 0px 17px;
cursor: pointer;
&:hover {
color: #d8d5d5;
background-color: #999696;
}
}
.leftBtn {
left: 0;
}
.rightBtn {
right: 0;
}
.menu-tag {
// width: 1000px;
height: 40px;
box-sizing: border-box;
// margin: 10px 16px;
display: flex;
align-items: center;
overflow: hidden;
justify-content: flex-start;
&>* {
flex-shrink: 0;
}
}
</style>
最后路由緩存需要頁面申明name
使用steup語法糖 可以安裝插件 vite-plugin-vue-setup-extend
vite.config.js內(nèi)配置
就可以直接
<script steup name="aaaa">