结构调整

This commit is contained in:
Shikong 2023-07-11 16:11:15 +08:00
parent 6952d6837c
commit 0500a5752b
7 changed files with 130 additions and 70 deletions

View File

@ -1,26 +1,17 @@
<template> <template>
<div class="mx-1"> <div class="mx-1">
<el-config-provider v-bind="globalConfig"> <el-config-provider v-bind="globalConfig">
<router-view v-slot="{ Component, route }">
<el-tabs v-model="globalTab" @tab-change="tabChange" @tab-remove="tabRemove"> <transition name="fade" mode="out-in">
<el-tab-pane <div>
v-for="item in globalTabs" <component v-if="route.meta.keepAlive != null && route.meta.keepAlive === false" :is="Component"
:key="item.name" :key="route.path"/>
:label="item.title" <keep-alive v-else>
:closable="item.closable" <component :is="Component" :key="route.path"/>
:name="item.name"> </keep-alive>
<router-view v-slot="{ Component, route }"> </div>
<transition name="fade" mode="out-in"> </transition>
<div> </router-view>
<component v-if="route.meta.keepAlive != null && route.meta.keepAlive === false" :is="Component" :key="route.path"/>
<keep-alive v-else>
<component :is="Component" :key="route.path"/>
</keep-alive>
</div>
</transition>
</router-view>
</el-tab-pane>
</el-tabs>
</el-config-provider> </el-config-provider>
</div> </div>
</template> </template>
@ -29,39 +20,10 @@
import {computed, onBeforeUnmount, onMounted} from "vue"; import {computed, onBeforeUnmount, onMounted} from "vue";
import {createDebugger, destroyDebugger} from "src/utils/debugger/eruda"; import {createDebugger, destroyDebugger} from "src/utils/debugger/eruda";
import {useGlobalConfig} from "src/store/globalConfig"; import {useGlobalConfig} from "src/store/globalConfig";
import {useGlobalTabs} from "src/store/globalTabs"; import {mapWritableState} from "pinia";
import {mapWritableState, storeToRefs} from "pinia";
import {useRoute, useRouter} from "vue-router";
const globalConfig = computed(mapWritableState(useGlobalConfig,['ui']).ui) const globalConfig = computed(mapWritableState(useGlobalConfig,['ui']).ui)
const globalTabsState = useGlobalTabs()
globalTabsState.$reset()
const {tab:globalTab, tabs:globalTabs} = storeToRefs(globalTabsState)
const router = useRouter()
const route = useRoute()
router.push({
path: "/"
})
function tabChange(name){
console.log("tabChange", name)
router.push({
path: name
})
}
function tabRemove(name){
let index = globalTabs.value.findIndex(item => item.name === name)
if(index >= globalTabs.value.length){
globalTab.value = globalTabs.value[0].name
} else {
globalTab.value = globalTabs.value[index+1].name
globalTabs.value.splice(index,1)
}
}
onMounted(()=>{ onMounted(()=>{
createDebugger() createDebugger()
}) })

View File

@ -0,0 +1,53 @@
<script setup>
import {useGlobalTabs} from "src/store/globalTabs";
import {storeToRefs} from "pinia";
import {useRoute, useRouter} from "vue-router";
const globalTabsState = useGlobalTabs()
const {tab, tabs} = storeToRefs(globalTabsState)
const router = useRouter()
const route = useRoute()
function tabChange(name){
console.log("tabChange", name)
router.push({
path: name
})
}
function tabRemove(name){
let index = tabs.value.findIndex(item => item.name === name)
if(index >= tabs.value.length){
tab.value = tabs.value[0].name
} else {
tab.value = tabs.value[index-1].name
tabs.value.splice(index,1)
}
}
</script>
<template>
<el-tabs v-model="tab" @tab-change="tabChange" @tab-remove="tabRemove">
<el-tab-pane
v-for="item in tabs"
:key="item.name"
:label="item.title"
:closable="item.closable"
:name="item.name">
<router-view v-slot="{ Component, route }">
<transition name="fade" mode="out-in">
<div>
<component v-if="route.meta.keepAlive != null && route.meta.keepAlive === false" :is="Component" :key="route.path"/>
<keep-alive v-else>
<component :is="Component" :key="route.path"/>
</keep-alive>
</div>
</transition>
</router-view>
</el-tab-pane>
</el-tabs>
</template>
<style scoped>
</style>

View File

@ -7,18 +7,28 @@ import * as VueRouter from "vue-router"
const routes = [ const routes = [
{ {
path: '/', path: '/',
component: ()=>import("frontend/src/views/Home.vue"), component: ()=>import("src/views/home/index.vue"),
meta: { meta: {
title: "首页" title: "首页",
} keepAlive: true,
},
children: [
{
path: "/home",
component: ()=>import("src/views/home/Home.vue"),
meta: {
title: "首页"
}
},
{
path: "/test",
component: ()=>import("src/views/home/Test.vue"),
meta: {
title: "测试"
}
}
]
}, },
{
path: "/test",
component: ()=>import("frontend/src/views/Test.vue"),
meta: {
title: "测试"
}
}
] ]
const Router = VueRouter.createRouter({ const Router = VueRouter.createRouter({

View File

@ -2,10 +2,17 @@ import {defineStore} from "pinia";
export const useGlobalTabs = defineStore("globalTabs",{ export const useGlobalTabs = defineStore("globalTabs",{
state:()=>({ state:()=>({
tab: "/", tab: "",
tabs: [{ tabs: []
name:"/", }),
title: "首页" actions:{
}] addTab(tab){
}) let index = this.tabs.findIndex(item => item.name === tab.name)
if( index === -1){
this.tabs.push(tab)
} else {
this.tabs[index] = Object.assign(this.tabs[index],tab)
}
}
}
}) })

View File

@ -1,7 +1,7 @@
<script setup> <script setup>
import {Greet} from 'frontend/wailsjs/go/main/App'; import {Greet} from 'frontend/wailsjs/go/main/App';
import {reactive} from "vue"; import {reactive} from "vue";
import {toReactive, useWebNotification} from '@vueuse/core' import {useWebNotification} from '@vueuse/core'
import {GetAllEnv} from "frontend/wailsjs/go/env/Env"; import {GetAllEnv} from "frontend/wailsjs/go/env/Env";
import {hideDebugger, showDebugger} from "src/utils/debugger/eruda"; import {hideDebugger, showDebugger} from "src/utils/debugger/eruda";
import {useGlobalConfig} from "src/store/globalConfig"; import {useGlobalConfig} from "src/store/globalConfig";
@ -89,14 +89,14 @@ function switchLocale(){
} }
const router = useRouter() const router = useRouter()
const globalTabState = storeToRefs(useGlobalTabs()) const globalTabState = useGlobalTabs()
function addTab(){ function addTab(){
globalTabState.tabs.value.unshift({ globalTabState.addTab({
name: "/test", name: "/test",
title: "测试", title: "测试",
closable: true, closable: true,
}) })
globalTabState.tab.value = "/test" globalTabState.tab = "/test"
} }
</script> </script>

View File

@ -0,0 +1,28 @@
<script setup>
import TabScaffold from "src/components/scaffold/TabScaffold.vue";
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import {useGlobalTabs} from "src/store/globalTabs";
const route = useRoute();
const router = useRouter();
const globalTabsState = useGlobalTabs()
globalTabsState.tab = route.path
if(globalTabsState.tabs.find(item => item.name === route.path) == null){
globalTabsState.tabs = [...new Set(globalTabsState.tabs).add({
name: route.path,
title: route.meta.title,
})]
}
if(route.path === "/"){
router.push("/home")
}
</script>
<template>
<TabScaffold></TabScaffold>
</template>
<style scoped>
</style>