perf: 优化 CompConsumer 代码

This commit is contained in:
pany 2023-08-28 19:12:35 +08:00
parent 28f1c0718b
commit 6b62edc323
2 changed files with 17 additions and 16 deletions

View File

@ -1,5 +1,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useTagsViewStore } from "@/store/modules/tags-view" import { useTagsViewStore } from "@/store/modules/tags-view"
// import { CompConsumer } from "./CompConsumer"
const tagsViewStore = useTagsViewStore() const tagsViewStore = useTagsViewStore()
</script> </script>
@ -12,6 +14,7 @@ const tagsViewStore = useTagsViewStore()
<keep-alive :include="tagsViewStore.cachedViews"> <keep-alive :include="tagsViewStore.cachedViews">
<component :is="Component" :key="route.path" /> <component :is="Component" :key="route.path" />
</keep-alive> </keep-alive>
<!-- <CompConsumer :component="Component" /> -->
</transition> </transition>
</router-view> </router-view>
</div> </div>

View File

@ -1,34 +1,33 @@
import { useTagsViewStore } from "@/store/modules/tags-view" import { type VNode, cloneVNode, createVNode, defineComponent, h, KeepAlive } from "vue"
import type { VNode } from "vue"
import { h } from "vue"
import { KeepAlive, cloneVNode, createVNode, defineComponent } from "vue"
import { useRoute } from "vue-router" import { useRoute } from "vue-router"
import { useTagsViewStore } from "@/store/modules/tags-view"
interface CompConsumerProps { interface CompConsumerProps {
component?: VNode component: VNode
} }
const compMap = new Map<string, VNode>() const compMap = new Map<string, VNode>()
export const CompConsumer = defineComponent( export const CompConsumer = defineComponent(
(props: CompConsumerProps) => { (props: CompConsumerProps) => {
const tagsViewStore = useTagsViewStore() const tagsViewStore = useTagsViewStore()
const route = useRoute() const route = useRoute()
return () => { return () => {
const component = props.component const component = props.component
// 判断当前是否包含name如果不包含name那就直接处理掉name // 判断当前是否包含 name如果不包含name那就直接处理掉 name
if (!route.name) return component if (!route.name) return component
// 获取当前组件的name // 获取当前组件的 name
const compName = (component?.type as any)?.name const compName = (component.type as any)?.name
const routeName = route.name as string const routeName = route.name as string
let Comp: VNode let Comp: VNode
if (compMap.has(routeName)) { if (compMap.has(routeName)) {
// @ts-expect-error this is Node Comp = compMap.get(routeName)!
Comp = compMap.get(routeName)
} else { } else {
const node = cloneVNode(component!) const node = cloneVNode(component)
if (compName && compName === routeName) if (compName && compName === routeName) {
// @ts-expect-error this is obj ;(node.type as any).name = `__${compName}__CUSTOM_NAME`
node.type.name = `__${compName}__` + "CUSTOM_NAME" }
// @ts-expect-error this is VNode // @ts-expect-error this is VNode
// eslint-disable-next-line vue/one-component-per-file
Comp = defineComponent({ Comp = defineComponent({
name: routeName, name: routeName,
setup() { setup() {
@ -47,7 +46,6 @@ export const CompConsumer = defineComponent(
} }
) )
} }
// eslint-disable-next-line vue/one-component-per-file
}, },
{ {
name: "CompConsumer", name: "CompConsumer",