@@ -55,6 +55,7 @@ export class History {
55
55
}
56
56
57
57
const {
58
+ updated,
58
59
deactivated,
59
60
activated
60
61
} = resolveQueue ( this . current . matched , route . matched )
@@ -64,7 +65,9 @@ export class History {
64
65
extractLeaveGuards ( deactivated ) ,
65
66
// global before hooks
66
67
this . router . beforeHooks ,
67
- // enter guards
68
+ // in-component update hooks
69
+ extractUpdateHooks ( updated ) ,
70
+ // in-config enter guards
68
71
activated . map ( m => m . beforeEnter ) ,
69
72
// async components
70
73
resolveAsyncComponents ( activated )
@@ -93,9 +96,8 @@ export class History {
93
96
94
97
runQueue ( queue , iterator , ( ) => {
95
98
const postEnterCbs = [ ]
96
- const enterGuards = extractEnterGuards ( activated , postEnterCbs , ( ) => {
97
- return this . current === route
98
- } )
99
+ const isValid = ( ) => this . current === route
100
+ const enterGuards = extractEnterGuards ( activated , postEnterCbs , isValid )
99
101
// wait until async components are resolved before
100
102
// extracting in-component enter guards
101
103
runQueue ( enterGuards , iterator , ( ) => {
@@ -145,6 +147,7 @@ function resolveQueue (
145
147
current : Array < RouteRecord > ,
146
148
next : Array < RouteRecord >
147
149
) : {
150
+ updated : Array < RouteRecord > ,
148
151
activated : Array < RouteRecord > ,
149
152
deactivated : Array < RouteRecord >
150
153
} {
@@ -156,11 +159,29 @@ function resolveQueue (
156
159
}
157
160
}
158
161
return {
162
+ updated : next . slice ( 0 , i ) ,
159
163
activated : next . slice ( i ) ,
160
164
deactivated : current . slice ( i )
161
165
}
162
166
}
163
167
168
+ function extractGuards (
169
+ records : Array < RouteRecord > ,
170
+ name : string ,
171
+ bind : Function ,
172
+ reverse ?: boolean
173
+ ) : Array < ?Function > {
174
+ const guards = flatMapComponents ( records , ( def , instance , match , key ) => {
175
+ const guard = extractGuard ( def , name )
176
+ if ( guard ) {
177
+ return Array . isArray ( guard )
178
+ ? guard . map ( guard => bind ( guard , instance , match , key ) )
179
+ : bind ( guard , instance , match , key )
180
+ }
181
+ } )
182
+ return flatten ( reverse ? guards . reverse ( ) : guards )
183
+ }
184
+
164
185
function extractGuard (
165
186
def : Object | Function ,
166
187
key : string
@@ -172,46 +193,35 @@ function extractGuard (
172
193
return def . options [ key ]
173
194
}
174
195
175
- function extractLeaveGuards ( matched : Array < RouteRecord > ) : Array < ?Function > {
176
- return flatten ( flatMapComponents ( matched , ( def , instance ) => {
177
- const guard = extractGuard ( def , 'beforeRouteLeave' )
178
- if ( guard ) {
179
- return Array . isArray ( guard )
180
- ? guard . map ( guard => wrapLeaveGuard ( guard , instance ) )
181
- : wrapLeaveGuard ( guard , instance )
182
- }
183
- } ) . reverse ( ) )
196
+ function extractLeaveGuards ( deactivated : Array < RouteRecord > ) : Array < ?Function > {
197
+ return extractGuards ( deactivated , 'beforeRouteLeave' , bindGuard , true )
184
198
}
185
199
186
- function wrapLeaveGuard (
187
- guard : NavigationGuard ,
188
- instance : _Vue
189
- ) : NavigationGuard {
190
- return function routeLeaveGuard ( ) {
200
+ function extractUpdateHooks ( updated : Array < RouteRecord > ) : Array < ?Function > {
201
+ return extractGuards ( updated , 'beforeRouteUpdate' , bindGuard )
202
+ }
203
+
204
+ function bindGuard ( guard : NavigationGuard , instance : _Vue ) : NavigationGuard {
205
+ return function boundRouteGuard ( ) {
191
206
return guard . apply ( instance , arguments )
192
207
}
193
208
}
194
209
195
210
function extractEnterGuards (
196
- matched : Array < RouteRecord > ,
211
+ activated : Array < RouteRecord > ,
197
212
cbs : Array < Function > ,
198
213
isValid : ( ) = > boolean
199
214
) : Array < ?Function > {
200
- return flatten ( flatMapComponents ( matched , ( def , _ , match , key ) => {
201
- const guard = extractGuard ( def , 'beforeRouteEnter' )
202
- if ( guard ) {
203
- return Array . isArray ( guard )
204
- ? guard . map ( guard => wrapEnterGuard ( guard , cbs , match , key , isValid ) )
205
- : wrapEnterGuard ( guard , cbs , match , key , isValid )
206
- }
207
- } ) )
215
+ return extractGuards ( activated , 'beforeRouteEnter' , ( guard , _ , match , key ) => {
216
+ return bindEnterGuard ( guard , match , key , cbs , isValid )
217
+ } )
208
218
}
209
219
210
- function wrapEnterGuard (
220
+ function bindEnterGuard (
211
221
guard : NavigationGuard ,
212
- cbs : Array < Function > ,
213
222
match : RouteRecord ,
214
223
key : string ,
224
+ cbs : Array < Function > ,
215
225
isValid : ( ) = > boolean
216
226
) : NavigationGuard {
217
227
return function routeEnterGuard ( to , from , next ) {
0 commit comments