@@ -4,7 +4,7 @@ import type VueRouter from '../index'
44import { warn } from '../util/warn'
55import { inBrowser } from '../util/dom'
66import { runQueue } from '../util/async'
7- import { createRoute , isSameRoute } from '../util/route'
7+ import { START , isSameRoute } from '../util/route'
88
99export class History {
1010 router : VueRouter ;
@@ -23,9 +23,7 @@ export class History {
2323 this . router = router
2424 this . base = normalizeBase ( base )
2525 // start with a route object that stands for "nowhere"
26- this . current = createRoute ( null , {
27- path : '__vue_router_init__'
28- } )
26+ this . current = START
2927 this . pending = null
3028 }
3129
@@ -43,7 +41,8 @@ export class History {
4341 }
4442
4543 confirmTransition ( route : Route , cb : Function ) {
46- if ( isSameRoute ( route , this . current ) ) {
44+ const current = this . current
45+ if ( isSameRoute ( route , current ) ) {
4746 this . ensureURL ( )
4847 return
4948 }
@@ -65,15 +64,28 @@ export class History {
6564 )
6665
6766 this . pending = route
68- const redirect = location => this . push ( location )
69- const iterator = ( hook , next ) => hook ( route , redirect , next )
67+ const iterator = ( hook , next ) => {
68+ if ( this . pending !== route ) return
69+ hook ( route , current , ( to : any ) => {
70+ if ( to === false ) {
71+ // next(false) -> abort navigation, ensure current URL
72+ this . ensureURL ( )
73+ } else if ( typeof to === 'string' || typeof to === 'object' ) {
74+ // next('/') or next({ path: '/' }) -> redirect
75+ this . push ( to )
76+ } else {
77+ // confirm transition and pass on the value
78+ next ( to )
79+ }
80+ } )
81+ }
7082
7183 runQueue ( queue , iterator , ( ) => {
7284 const postEnterCbs = [ ]
7385 // wait until async components are resolved before
7486 // extracting in-component enter guards
7587 runQueue ( extractEnterGuards ( activated , postEnterCbs ) , iterator , ( ) => {
76- if ( isSameRoute ( route , this . pending ) ) {
88+ if ( this . pending === route ) {
7789 this . pending = null
7890 cb ( route )
7991 this . router . app . $nextTick ( ( ) => {
@@ -146,12 +158,14 @@ function extractEnterGuards (matched: Array<RouteRecord>, cbs: Array<Function>):
146158 return flatMapComponents ( matched , ( def , _ , match , key ) => {
147159 const guard = def && def . beforeRouteEnter
148160 if ( guard ) {
149- return function routeEnterGuard ( route , redirect , next ) {
150- return guard ( route , redirect , cb => {
151- next ( )
152- cb && cbs . push ( ( ) => {
153- cb ( match . instances [ key ] )
154- } )
161+ return function routeEnterGuard ( to , from , next ) {
162+ return guard ( to , from , cb => {
163+ next ( cb )
164+ if ( typeof cb === 'function' ) {
165+ cbs . push ( ( ) => {
166+ cb ( match . instances [ key ] )
167+ } )
168+ }
155169 } )
156170 }
157171 }
@@ -166,14 +180,15 @@ function resolveAsyncComponents (matched: Array<RouteRecord>): Array<?Function>
166180 // we want to halt the navigation until the incoming component has been
167181 // resolved.
168182 if ( typeof def === 'function' && ! def . options ) {
169- return ( route , redirect , next ) => {
183+ return ( to , from , next ) => {
170184 const resolve = resolvedDef => {
171185 match . components [ key ] = resolvedDef
172186 next ( )
173187 }
174188
175189 const reject = reason => {
176190 warn ( false , `Failed to resolve async component ${ key } : ${ reason } ` )
191+ next ( false )
177192 }
178193
179194 const res = def ( resolve , reject )
0 commit comments