1
- import { defineComponent , h , resolveComponent } from 'vue'
1
+ import { defineComponent , h , onMounted , ref , resolveComponent } from 'vue'
2
2
import { RouterLink , useRoute } from 'vue-router'
3
3
4
4
import { CBadge , CSidebarNav , CNavItem , CNavGroup , CNavTitle } from '@coreui/vue'
5
5
import nav from '@/_nav.js'
6
6
7
+ const normalizePath = ( path ) =>
8
+ decodeURI ( path )
9
+ . replace ( / # .* $ / , '' )
10
+ . replace ( / ( i n d e x ) ? \. ( h t m l ) $ / , '' )
11
+
12
+ const isActiveLink = ( route , link ) => {
13
+ if ( link === undefined ) {
14
+ return false
15
+ }
16
+
17
+ if ( route . hash === link ) {
18
+ return true
19
+ }
20
+
21
+ const currentPath = normalizePath ( route . path )
22
+ const targetPath = normalizePath ( link )
23
+
24
+ return currentPath === targetPath
25
+ }
26
+
27
+ const isActiveItem = ( route , item ) => {
28
+ if ( isActiveLink ( route , item . to ) ) {
29
+ return true
30
+ }
31
+
32
+ if ( item . items ) {
33
+ return item . items . some ( ( child ) => isActiveItem ( route , child ) )
34
+ }
35
+
36
+ return false
37
+ }
38
+
7
39
const AppSidebarNav = defineComponent ( {
8
40
name : 'AppSidebarNav' ,
9
41
components : {
@@ -12,46 +44,19 @@ const AppSidebarNav = defineComponent({
12
44
CNavTitle,
13
45
} ,
14
46
setup ( ) {
15
- const normalizePath = ( path ) =>
16
- decodeURI ( path )
17
- . replace ( / # .* $ / , '' )
18
- . replace ( / ( i n d e x ) ? \. ( h t m l ) $ / , '' )
19
-
20
- const isActiveLink = ( route , link ) => {
21
- if ( link === undefined ) {
22
- return false
23
- }
24
-
25
- if ( route . hash === link ) {
26
- return true
27
- }
28
-
29
- const currentPath = normalizePath ( route . path )
30
- const targetPath = normalizePath ( link )
31
-
32
- return currentPath === targetPath
33
- }
47
+ const route = useRoute ( )
48
+ const firstRender = ref ( true )
34
49
35
- const isActiveItem = ( route , item ) => {
36
- if ( isActiveLink ( route , item . to ) ) {
37
- return true
38
- }
39
-
40
- if ( item . items ) {
41
- return item . items . some ( ( child ) => isActiveItem ( route , child ) )
42
- }
43
-
44
- return false
45
- }
50
+ onMounted ( ( ) => {
51
+ firstRender . value = false
52
+ } )
46
53
47
54
const renderItem = ( item ) => {
48
- const route = useRoute ( )
49
-
50
55
if ( item . items ) {
51
56
return h (
52
57
CNavGroup ,
53
58
{
54
- active : item . items . some ( ( child ) => isActiveItem ( route , child ) ) ,
59
+ ... firstRender . value && { visible : item . items . some ( ( child ) => isActiveItem ( route , child ) ) }
55
60
} ,
56
61
{
57
62
togglerContent : ( ) => [
0 commit comments