mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
fix: 打开相同path的链接会更新route & 第一次打开会触发onActivated (#145)
* fix: 打开相同path的链接会更新route & 第一次打开会触发onActivated * fix: 优化
This commit is contained in:
parent
2b0defc918
commit
864904e68d
@ -32,16 +32,13 @@
|
|||||||
</FTabs>
|
</FTabs>
|
||||||
<router-view v-slot="{ Component, route }">
|
<router-view v-slot="{ Component, route }">
|
||||||
<keep-alive :include="keepAlivePages">
|
<keep-alive :include="keepAlivePages">
|
||||||
<component
|
<component :is="Component" :key="getPageKey(route)" />
|
||||||
:is="getComponent(Component, route, true)"
|
|
||||||
:key="getPageKey(route)"
|
|
||||||
/>
|
|
||||||
</keep-alive>
|
</keep-alive>
|
||||||
</router-view>
|
</router-view>
|
||||||
</template>
|
</template>
|
||||||
<router-view v-else v-slot="{ Component, route }">
|
<router-view v-else v-slot="{ Component }">
|
||||||
<keep-alive :include="keepAlivePages">
|
<keep-alive :include="keepAlivePages">
|
||||||
<component :is="getComponent(Component, route)" />
|
<component :is="Component" />
|
||||||
</keep-alive>
|
</keep-alive>
|
||||||
</router-view>
|
</router-view>
|
||||||
</template>
|
</template>
|
||||||
@ -65,7 +62,10 @@ export default {
|
|||||||
props: {
|
props: {
|
||||||
multiTabs: Boolean
|
multiTabs: Boolean
|
||||||
},
|
},
|
||||||
setup() {
|
setup(props) {
|
||||||
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
const createPage = (_route) => {
|
const createPage = (_route) => {
|
||||||
const title = _route.meta.title;
|
const title = _route.meta.title;
|
||||||
return {
|
return {
|
||||||
@ -76,11 +76,28 @@ export default {
|
|||||||
key: getKey()
|
key: getKey()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
const keepAlivePages = ref([]);
|
|
||||||
|
|
||||||
const route = useRoute();
|
function changePageComName(_route) {
|
||||||
const router = useRouter();
|
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 pageList = ref([createPage(route)]);
|
||||||
|
const keepAlivePages = ref(getInitAlivePage());
|
||||||
const actions = [
|
const actions = [
|
||||||
{
|
{
|
||||||
value: 'closeOtherPage',
|
value: 'closeOtherPage',
|
||||||
@ -95,11 +112,24 @@ export default {
|
|||||||
const findPage = path => pageList.value.find(item => unref(item.path) === unref(path));
|
const findPage = path => pageList.value.find(item => unref(item.path) === unref(path));
|
||||||
|
|
||||||
router.beforeEach((to) => {
|
router.beforeEach((to) => {
|
||||||
if (!findPage(to.path)) {
|
const page = findPage(to.path);
|
||||||
|
if (!page) {
|
||||||
pageList.value = [...pageList.value, createPage(to)];
|
pageList.value = [...pageList.value, createPage(to)];
|
||||||
|
} else {
|
||||||
|
page.route = to;
|
||||||
}
|
}
|
||||||
return true;
|
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 switchPage = async (path) => {
|
||||||
const selectedPage = findPage(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 {
|
return {
|
||||||
route,
|
route,
|
||||||
pageList,
|
pageList,
|
||||||
@ -187,7 +202,6 @@ export default {
|
|||||||
handlerMore,
|
handlerMore,
|
||||||
handleCloseTab,
|
handleCloseTab,
|
||||||
actions,
|
actions,
|
||||||
getComponent,
|
|
||||||
keepAlivePages
|
keepAlivePages
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ export default {
|
|||||||
layout: {
|
layout: {
|
||||||
title: 'Fes.js',
|
title: 'Fes.js',
|
||||||
footer: 'Created by MumbleFE',
|
footer: 'Created by MumbleFE',
|
||||||
multiTabs: false,
|
multiTabs: true,
|
||||||
navigation: 'side',
|
navigation: 'side',
|
||||||
theme: 'dark',
|
theme: 'dark',
|
||||||
menus: [
|
menus: [
|
||||||
|
@ -22,7 +22,9 @@
|
|||||||
}
|
}
|
||||||
</config>
|
</config>
|
||||||
<script>
|
<script>
|
||||||
import { computed, ref } from 'vue';
|
import {
|
||||||
|
computed, ref, onMounted, onUnmounted, onActivated, onDeactivated
|
||||||
|
} from 'vue';
|
||||||
import { useStore } from 'vuex';
|
import { useStore } from 'vuex';
|
||||||
import { MUTATION_TYPES, GETTER_TYPES, ACTION_TYPES } from '@fesjs/fes';
|
import { MUTATION_TYPES, GETTER_TYPES, ACTION_TYPES } from '@fesjs/fes';
|
||||||
|
|
||||||
@ -32,6 +34,23 @@ export default {
|
|||||||
const store = useStore();
|
const store = useStore();
|
||||||
console.log('store==>', store);
|
console.log('store==>', store);
|
||||||
const disabled = ref(false);
|
const disabled = ref(false);
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
console.log('onMounted');
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
console.log('onUnmounted');
|
||||||
|
});
|
||||||
|
|
||||||
|
onActivated(() => {
|
||||||
|
console.log('onActivated');
|
||||||
|
});
|
||||||
|
|
||||||
|
onDeactivated(() => {
|
||||||
|
console.log('onDeactivated');
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
address: computed(() => store.getters[GETTER_TYPES.user.address]),
|
address: computed(() => store.getters[GETTER_TYPES.user.address]),
|
||||||
doubleCount: computed(
|
doubleCount: computed(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user