forked from un-pany/v3-admin-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
张浩杰
committed
Aug 24, 2023
1 parent
4c0fcd0
commit 740203e
Showing
2 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useTagsViewStore } from "@/store/modules/tags-view" | ||
import type { VNode } from "vue" | ||
import { h } from "vue" | ||
import { KeepAlive, cloneVNode, createVNode, defineComponent } from "vue" | ||
import { useRoute } from "vue-router" | ||
interface CompConsumerProps { | ||
component?: VNode | ||
} | ||
const compMap = new Map<string, VNode>() | ||
export const CompConsumer = defineComponent( | ||
(props: CompConsumerProps) => { | ||
const tagsViewStore = useTagsViewStore() | ||
const route = useRoute() | ||
return () => { | ||
const component = props.component | ||
// 判断当前是否包含name,如果不包含name,那就直接处理掉name | ||
if (!route.name) return component | ||
// 获取当前组件的name | ||
const compName = (component?.type as any)?.name | ||
const routeName = route.name as string | ||
let Comp: VNode | ||
if (compMap.has(routeName)) { | ||
// @ts-expect-error this is Node | ||
Comp = compMap.get(routeName) | ||
} else { | ||
const node = cloneVNode(component!) | ||
if (compName && compName === routeName) | ||
// @ts-expect-error this is obj | ||
node.type.name = `__${compName}__` + "CUSTOM_NAME" | ||
// @ts-expect-error this is VNode | ||
// eslint-disable-next-line vue/one-component-per-file | ||
Comp = defineComponent({ | ||
name: routeName, | ||
setup() { | ||
return () => node | ||
} | ||
}) | ||
compMap.set(routeName, Comp) | ||
} | ||
return createVNode( | ||
KeepAlive, | ||
{ | ||
include: tagsViewStore.cachedViews | ||
}, | ||
{ | ||
default: () => h(Comp) | ||
} | ||
) | ||
} | ||
// eslint-disable-next-line vue/one-component-per-file | ||
}, | ||
{ | ||
name: "CompConsumer", | ||
props: ["component"] | ||
} | ||
) |