Skip to content

Commit 7f2c172

Browse files
committed
feat(hooks): use-exact-view 的 exact 作为函数时传入 viewDepth
1 parent 898f19b commit 7f2c172

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/hooks/use-exact-view.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { MaybeRefOrGetter, Reactive } from 'vue'
1+
import type { ComputedRef, MaybeRef, Reactive } from 'vue'
22
import { computed, inject, provide, toValue } from 'vue'
33
import { useRoute, viewDepthKey } from 'vue-router'
4-
import { isBoolean } from '../utils'
4+
import { isBoolean, isFunction } from '../utils'
5+
6+
export type ExactValue = boolean | number | null
57

68
export interface UseExactViewOptions {
79
/**
@@ -10,7 +12,7 @@ export interface UseExactViewOptions {
1012
* - number: 设置为数字时,将精准匹配对应路由的视图组件,且在调用该函数的组件下所有视图组件**支持再嵌套** `<RouterView>`!
1113
* - null: 默认值,将还原 `<RouterView>` 渲染逻辑
1214
*/
13-
exact: MaybeRefOrGetter<boolean | number | null>
15+
exact: MaybeRef<ExactValue> | ComputedRef<ExactValue> | ((viewDepth: number) => ExactValue)
1416
}
1517

1618
export function useExactView(options: UseExactViewOptions | Reactive<UseExactViewOptions>): void {
@@ -19,8 +21,10 @@ export function useExactView(options: UseExactViewOptions | Reactive<UseExactVie
1921
provide(
2022
viewDepthKey,
2123
computed(() => {
22-
const exactValue = toValue(options.exact)
2324
const viewDepthValue = toValue(viewDepth)
25+
const exactValue = isFunction(options.exact)
26+
? options.exact(viewDepthValue)
27+
: toValue(options.exact)
2428

2529
if (exactValue == null) {
2630
return viewDepthValue

0 commit comments

Comments
 (0)