3
3
* (c) 2022 Evan You
4
4
* @license MIT
5
5
*/
6
- 'use strict' ;
6
+ 'use strict'
7
7
8
- Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
8
+ Object . defineProperty ( exports , '__esModule' , { value : true } )
9
9
10
- var vue = require ( 'vue' ) ;
10
+ var vue = require ( 'vue' )
11
11
12
12
// dev only warn if no current instance
13
13
14
14
function throwNoCurrentInstance ( method ) {
15
15
if ( ! vue . getCurrentInstance ( ) ) {
16
16
throw new Error (
17
- ( " [vue-router]: Missing current instance. " + method + " () must be called inside <script setup> or setup()." )
17
+ ( ' [vue-router]: Missing current instance. ' + method + ' () must be called inside <script setup> or setup().' )
18
18
)
19
19
}
20
20
}
21
21
22
22
function useRouter ( ) {
23
23
if ( process . env . NODE_ENV !== 'production' ) {
24
- throwNoCurrentInstance ( 'useRouter' ) ;
24
+ throwNoCurrentInstance ( 'useRouter' )
25
25
}
26
26
27
27
return vue . getCurrentInstance ( ) . proxy . $root . $router
28
28
}
29
29
30
30
function useRoute ( ) {
31
31
if ( process . env . NODE_ENV !== 'production' ) {
32
- throwNoCurrentInstance ( 'useRoute' ) ;
32
+ throwNoCurrentInstance ( 'useRoute' )
33
33
}
34
34
35
- var root = vue . getCurrentInstance ( ) . proxy . $root ;
35
+ var root = vue . getCurrentInstance ( ) . proxy . $root
36
36
if ( ! root . _$route ) {
37
- var route = vue . effectScope ( true ) . run ( function ( ) { return vue . shallowReactive ( Object . assign ( { } , root . $router . currentRoute ) ) ; }
38
- ) ;
39
- root . _$route = route ;
37
+ var route = vue . effectScope ( true ) . run ( function ( ) { return vue . shallowReactive ( Object . assign ( { } , root . $router . currentRoute ) ) }
38
+ )
39
+ root . _$route = route
40
40
41
41
root . $router . afterEach ( function ( to ) {
42
- Object . assign ( route , to ) ;
43
- } ) ;
42
+ Object . assign ( route , to )
43
+ } )
44
44
}
45
45
46
46
return root . _$route
47
47
}
48
48
49
49
function onBeforeRouteUpdate ( guard ) {
50
50
if ( process . env . NODE_ENV !== 'production' ) {
51
- throwNoCurrentInstance ( 'onBeforeRouteUpdate' ) ;
51
+ throwNoCurrentInstance ( 'onBeforeRouteUpdate' )
52
52
}
53
53
54
54
return useFilteredGuard ( guard , isUpdateNavigation )
55
55
}
56
56
function isUpdateNavigation ( to , from , depth ) {
57
- var toMatched = to . matched ;
58
- var fromMatched = from . matched ;
57
+ var toMatched = to . matched
58
+ var fromMatched = from . matched
59
59
return (
60
60
toMatched . length >= depth &&
61
61
toMatched
62
62
. slice ( 0 , depth + 1 )
63
- . every ( function ( record , i ) { return record === fromMatched [ i ] ; } )
63
+ . every ( function ( record , i ) { return record === fromMatched [ i ] } )
64
64
)
65
65
}
66
66
67
67
function isLeaveNavigation ( to , from , depth ) {
68
- var toMatched = to . matched ;
69
- var fromMatched = from . matched ;
68
+ var toMatched = to . matched
69
+ var fromMatched = from . matched
70
70
return toMatched . length < depth || toMatched [ depth ] !== fromMatched [ depth ]
71
71
}
72
72
73
73
function onBeforeRouteLeave ( guard ) {
74
74
if ( process . env . NODE_ENV !== 'production' ) {
75
- throwNoCurrentInstance ( 'onBeforeRouteLeave' ) ;
75
+ throwNoCurrentInstance ( 'onBeforeRouteLeave' )
76
76
}
77
77
78
78
return useFilteredGuard ( guard , isLeaveNavigation )
79
79
}
80
80
81
- var noop = function ( ) { } ;
81
+ var noop = function ( ) { }
82
82
function useFilteredGuard ( guard , fn ) {
83
- var instance = vue . getCurrentInstance ( ) ;
84
- var router = useRouter ( ) ;
83
+ var instance = vue . getCurrentInstance ( )
84
+ var router = useRouter ( )
85
85
86
- var target = instance . proxy ;
86
+ var target = instance . proxy
87
87
// find the nearest RouterView to know the depth
88
88
while (
89
89
target &&
90
90
target . $vnode &&
91
91
target . $vnode . data &&
92
92
target . $vnode . data . routerViewDepth == null
93
93
) {
94
- target = target . $parent ;
94
+ target = target . $parent
95
95
}
96
96
97
97
var depth =
98
98
target && target . $vnode && target . $vnode . data
99
99
? target . $vnode . data . routerViewDepth
100
- : null ;
100
+ : null
101
101
102
102
if ( depth != null ) {
103
103
var removeGuard = router . beforeEach ( function ( to , from , next ) {
104
104
return fn ( to , from , depth ) ? guard ( to , from , next ) : next ( )
105
- } ) ;
105
+ } )
106
106
107
- vue . onUnmounted ( removeGuard ) ;
107
+ vue . onUnmounted ( removeGuard )
108
108
return removeGuard
109
109
}
110
110
@@ -122,37 +122,37 @@ function guardEvent (e) {
122
122
if ( e . button !== undefined && e . button !== 0 ) { return }
123
123
// don't redirect if `target="_blank"`
124
124
if ( e . currentTarget && e . currentTarget . getAttribute ) {
125
- var target = e . currentTarget . getAttribute ( 'target' ) ;
125
+ var target = e . currentTarget . getAttribute ( 'target' )
126
126
if ( / \b _ b l a n k \b / i. test ( target ) ) { return }
127
127
}
128
128
// this may be a Weex event which doesn't have this method
129
129
if ( e . preventDefault ) {
130
- e . preventDefault ( ) ;
130
+ e . preventDefault ( )
131
131
}
132
132
return true
133
133
}
134
134
135
135
function includesParams ( outer , inner ) {
136
- var loop = function ( key ) {
137
- var innerValue = inner [ key ] ;
138
- var outerValue = outer [ key ] ;
136
+ var loop = function ( key ) {
137
+ var innerValue = inner [ key ]
138
+ var outerValue = outer [ key ]
139
139
if ( typeof innerValue === 'string' ) {
140
140
if ( innerValue !== outerValue ) { return { v : false } }
141
141
} else {
142
142
if (
143
143
! Array . isArray ( outerValue ) ||
144
144
outerValue . length !== innerValue . length ||
145
- innerValue . some ( function ( value , i ) { return value !== outerValue [ i ] ; } )
145
+ innerValue . some ( function ( value , i ) { return value !== outerValue [ i ] } )
146
146
) {
147
147
return { v : false }
148
148
}
149
149
}
150
- } ;
150
+ }
151
151
152
152
for ( var key in inner ) {
153
- var returned = loop ( key ) ;
153
+ var returned = loop ( key )
154
154
155
- if ( returned ) return returned . v ;
155
+ if ( returned ) return returned . v
156
156
}
157
157
158
158
return true
@@ -170,7 +170,7 @@ function isSameRouteLocationParamsValue (a, b) {
170
170
171
171
function isEquivalentArray ( a , b ) {
172
172
return Array . isArray ( b )
173
- ? a . length === b . length && a . every ( function ( value , i ) { return value === b [ i ] ; } )
173
+ ? a . length === b . length && a . every ( function ( value , i ) { return value === b [ i ] } )
174
174
: a . length === 1 && a [ 0 ] === b
175
175
}
176
176
@@ -186,25 +186,25 @@ function isSameRouteLocationParams (a, b) {
186
186
187
187
function useLink ( props ) {
188
188
if ( process . env . NODE_ENV !== 'production' ) {
189
- throwNoCurrentInstance ( 'useLink' ) ;
189
+ throwNoCurrentInstance ( 'useLink' )
190
190
}
191
191
192
- var router = useRouter ( ) ;
193
- var currentRoute = useRoute ( ) ;
192
+ var router = useRouter ( )
193
+ var currentRoute = useRoute ( )
194
194
195
- var resolvedRoute = vue . computed ( function ( ) { return router . resolve ( vue . unref ( props . to ) , currentRoute ) ; } ) ;
195
+ var resolvedRoute = vue . computed ( function ( ) { return router . resolve ( vue . unref ( props . to ) , currentRoute ) } )
196
196
197
197
var activeRecordIndex = vue . computed ( function ( ) {
198
- var route = resolvedRoute . value . route ;
199
- var matched = route . matched ;
200
- var length = matched . length ;
201
- var routeMatched = matched [ length - 1 ] ;
202
- var currentMatched = currentRoute . matched ;
198
+ var route = resolvedRoute . value . route
199
+ var matched = route . matched
200
+ var length = matched . length
201
+ var routeMatched = matched [ length - 1 ]
202
+ var currentMatched = currentRoute . matched
203
203
if ( ! routeMatched || ! currentMatched . length ) { return - 1 }
204
- var index = currentMatched . indexOf ( routeMatched ) ;
204
+ var index = currentMatched . indexOf ( routeMatched )
205
205
if ( index > - 1 ) { return index }
206
206
// possible parent record
207
- var parentRecord = currentMatched [ currentMatched . length - 2 ] ;
207
+ var parentRecord = currentMatched [ currentMatched . length - 2 ]
208
208
209
209
return (
210
210
// we are dealing with nested routes
@@ -214,40 +214,44 @@ function useLink (props) {
214
214
// child of the same parent
215
215
parentRecord && parentRecord === routeMatched . parent
216
216
)
217
- } ) ;
217
+ } )
218
218
219
219
var isActive = vue . computed (
220
- function ( ) { return activeRecordIndex . value > - 1 &&
221
- includesParams ( currentRoute . params , resolvedRoute . value . route . params ) ; }
222
- ) ;
220
+ function ( ) {
221
+ return activeRecordIndex . value > - 1 &&
222
+ includesParams ( currentRoute . params , resolvedRoute . value . route . params )
223
+ }
224
+ )
223
225
var isExactActive = vue . computed (
224
- function ( ) { return activeRecordIndex . value > - 1 &&
226
+ function ( ) {
227
+ return activeRecordIndex . value > - 1 &&
225
228
activeRecordIndex . value === currentRoute . matched . length - 1 &&
226
- isSameRouteLocationParams ( currentRoute . params , resolvedRoute . value . route . params ) ; }
227
- ) ;
229
+ isSameRouteLocationParams ( currentRoute . params , resolvedRoute . value . route . params )
230
+ }
231
+ )
228
232
229
233
var navigate = function ( e ) {
230
- var href = resolvedRoute . value . route ;
234
+ var href = resolvedRoute . value . route
231
235
if ( guardEvent ( e ) ) {
232
236
return props . replace
233
237
? router . replace ( href )
234
238
: router . push ( href )
235
239
}
236
240
return Promise . resolve ( )
237
- } ;
241
+ }
238
242
239
243
return {
240
- href : vue . computed ( function ( ) { return resolvedRoute . value . href ; } ) ,
241
- route : vue . computed ( function ( ) { return resolvedRoute . value . route ; } ) ,
244
+ href : vue . computed ( function ( ) { return resolvedRoute . value . href } ) ,
245
+ route : vue . computed ( function ( ) { return resolvedRoute . value . route } ) ,
242
246
isExactActive : isExactActive ,
243
247
isActive : isActive ,
244
248
navigate : navigate
245
249
}
246
250
}
247
251
248
- exports . isSameRouteLocationParams = isSameRouteLocationParams ;
249
- exports . onBeforeRouteLeave = onBeforeRouteLeave ;
250
- exports . onBeforeRouteUpdate = onBeforeRouteUpdate ;
251
- exports . useLink = useLink ;
252
- exports . useRoute = useRoute ;
253
- exports . useRouter = useRouter ;
252
+ exports . isSameRouteLocationParams = isSameRouteLocationParams
253
+ exports . onBeforeRouteLeave = onBeforeRouteLeave
254
+ exports . onBeforeRouteUpdate = onBeforeRouteUpdate
255
+ exports . useLink = useLink
256
+ exports . useRoute = useRoute
257
+ exports . useRouter = useRouter
0 commit comments