From 864904e68d5dcb3d7c61ec2e69ecfd6224541f6f Mon Sep 17 00:00:00 2001 From: harrywan <445436867@qq.com> Date: Mon, 8 Aug 2022 10:21:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=93=E5=BC=80=E7=9B=B8=E5=90=8Cpath?= =?UTF-8?q?=E7=9A=84=E9=93=BE=E6=8E=A5=E4=BC=9A=E6=9B=B4=E6=96=B0route=20&?= =?UTF-8?q?=20=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=89=93=E5=BC=80=E4=BC=9A?= =?UTF-8?q?=E8=A7=A6=E5=8F=91onActivated=20(#145)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 打开相同path的链接会更新route & 第一次打开会触发onActivated * fix: 优化 --- .../src/runtime/views/MultiTabProvider.vue | 68 +++++++++++-------- packages/fes-template/.fes.js | 2 +- packages/fes-template/src/pages/store.vue | 21 +++++- 3 files changed, 62 insertions(+), 29 deletions(-) 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 @@ }