@@ -7,21 +7,14 @@ import type {
77} from 'vue'
88import type {
99 RouteComponent ,
10+ RouteLocationMatched ,
1011 RouteLocationNormalizedLoaded ,
1112 RouteLocationNormalizedLoadedGeneric ,
1213 RouterViewProps ,
1314} from 'vue-router'
14- import {
15- computed ,
16- defineComponent ,
17- getCurrentInstance ,
18- h ,
19- inject ,
20- provide ,
21- shallowRef ,
22- toValue ,
23- } from 'vue'
24- import { RouterView , useRoute , viewDepthKey } from 'vue-router'
15+ import { computed , defineComponent , getCurrentInstance , h , shallowRef } from 'vue'
16+ import { RouterView } from 'vue-router'
17+ import { useExactView } from './hooks/use-exact-view'
2518import { getWrappers } from './wrappers'
2619
2720export interface SlotData {
@@ -33,28 +26,26 @@ export type ResolveViewKey = (
3326 route : RouteLocationNormalizedLoaded ,
3427) => string | void | undefined | null
3528
29+ export type ExactFn = ( matchedRoute : RouteLocationMatched ) => boolean
30+
3631export interface BetterRouterViewProps extends RouterViewProps {
3732 resolveViewKey ?: ResolveViewKey
38- exact ?: boolean
33+ exact ?: boolean | number | null
3934}
4035
41- export const BetterRouterView : new ( ) => {
42- $props : AllowedComponentProps & ComponentCustomProps & VNodeProps & BetterRouterViewProps
43- $slots : {
44- default ?: ( data : SlotData ) => VNode [ ]
45- }
46- } = /* #__PURE__ */ defineComponent ( {
36+ const BetterRouterViewImpl = /* #__PURE__ */ defineComponent ( {
4737 name : 'BetterRouterView' ,
4838 inheritAttrs : false ,
4939 props : {
5040 resolveViewKey : {
5141 type : Function ,
5242 } ,
5343 exact : {
54- type : Boolean ,
44+ type : [ Boolean , Number , null ] ,
45+ default : null ,
5546 } ,
5647 } ,
57- setup ( props : BetterRouterViewProps , { attrs, slots } ) {
48+ setup ( props , { attrs, slots } ) {
5849 const app = getCurrentInstance ( ) ! . appContext . app
5950 const wrappers = getWrappers ( app )
6051
@@ -89,13 +80,9 @@ export const BetterRouterView: new () => {
8980 return wrappers . get ( name ) !
9081 }
9182
92- const route = useRoute ( )
93- const viewDepth = inject ( viewDepthKey , 0 )
94- provide (
95- viewDepthKey ,
96- // route.matched 最后一个往往就是精确匹配的,这里更改 viewDepth 后可以让其直接渲染对应的视图组件
97- computed ( ( ) => ( props . exact ? route . matched . length - 1 : toValue ( viewDepth ) ) ) ,
98- )
83+ useExactView ( {
84+ exact : computed ( ( ) => props . exact ) ,
85+ } )
9986
10087 return ( ) =>
10188 h ( RouterView , attrs , {
@@ -112,3 +99,10 @@ export const BetterRouterView: new () => {
11299 } )
113100 } ,
114101} )
102+
103+ export const BetterRouterView = BetterRouterViewImpl as new ( ) => {
104+ $props : AllowedComponentProps & ComponentCustomProps & VNodeProps & BetterRouterViewProps
105+ $slots : {
106+ default ?: ( data : SlotData ) => VNode [ ]
107+ }
108+ }
0 commit comments