diff --git a/packages/fes-plugin-layout/src/runtime/views/MultiTabProvider.vue b/packages/fes-plugin-layout/src/runtime/views/MultiTabProvider.vue index c3e027c6..8eccaa54 100644 --- a/packages/fes-plugin-layout/src/runtime/views/MultiTabProvider.vue +++ b/packages/fes-plugin-layout/src/runtime/views/MultiTabProvider.vue @@ -32,16 +32,13 @@ - + - + - + @@ -65,7 +62,10 @@ export default { props: { multiTabs: Boolean }, - setup() { + setup(props) { + const route = useRoute(); + const router = useRouter(); + const createPage = (_route) => { const title = _route.meta.title; return { @@ -76,11 +76,28 @@ export default { key: getKey() }; }; - const keepAlivePages = ref([]); - const route = useRoute(); - const router = useRouter(); + function changePageComName(_route) { + if (_route.meta['keep-alive'] || props.multiTabs) { + const matched = _route.matched; + const component = matched[matched.length - 1].components.default; + const name = _route.meta?.name ?? _route.name; + if (name && component) { + // 修改组件的 name + // 缓存的关键是组件name在keep-alive的include列表 + component.name = name; + return name; + } + } + } + + function getInitAlivePage() { + const name = changePageComName(route); + return name ? [name] : []; + } + const pageList = ref([createPage(route)]); + const keepAlivePages = ref(getInitAlivePage()); const actions = [ { value: 'closeOtherPage', @@ -95,11 +112,24 @@ export default { const findPage = path => pageList.value.find(item => unref(item.path) === unref(path)); router.beforeEach((to) => { - if (!findPage(to.path)) { + const page = findPage(to.path); + if (!page) { pageList.value = [...pageList.value, createPage(to)]; + } else { + page.route = to; } return true; }); + + router.afterEach(() => { + // 此时route已变,但是页面还未加载 + const name = changePageComName(route); + // 缓存的关键是组件name在keep-alive的include列表 + if (!keepAlivePages.value.includes(name)) { + keepAlivePages.value = [...keepAlivePages.value, name]; + } + }); + // 还需要考虑参数 const switchPage = async (path) => { const selectedPage = findPage(path); @@ -163,21 +193,6 @@ export default { } }; - const getComponent = (Component, _route, isKeep = false) => { - if (isKeep || _route.meta['keep-alive']) { - const name = _route.meta?.name ?? _route.name; - if (name) { - // 修改组件的 name - Component.type.name = name; - // 缓存的关键是组件name在keep-alive的include列表 - if (!keepAlivePages.value.includes(name)) { - keepAlivePages.value = [...keepAlivePages.value, name]; - } - } - } - - return Component; - }; return { route, pageList, @@ -187,7 +202,6 @@ export default { handlerMore, handleCloseTab, actions, - getComponent, keepAlivePages }; } diff --git a/packages/fes-template/.fes.js b/packages/fes-template/.fes.js index 0ac088fa..2d2b290b 100644 --- a/packages/fes-template/.fes.js +++ b/packages/fes-template/.fes.js @@ -36,7 +36,7 @@ export default { layout: { title: 'Fes.js', footer: 'Created by MumbleFE', - multiTabs: false, + multiTabs: true, navigation: 'side', theme: 'dark', menus: [ diff --git a/packages/fes-template/src/pages/store.vue b/packages/fes-template/src/pages/store.vue index bcb1a939..bfb4d550 100644 --- a/packages/fes-template/src/pages/store.vue +++ b/packages/fes-template/src/pages/store.vue @@ -22,7 +22,9 @@ }