File tree 4 files changed +26
-13
lines changed
4 files changed +26
-13
lines changed Original file line number Diff line number Diff line change @@ -11,17 +11,18 @@ export class History {
11
11
base: string ;
12
12
current: Route ;
13
13
pending: ?Route ;
14
- cb: Function ;
14
+ cb: ( r : Route ) => void ;
15
15
16
16
// implemented by sub-classes
17
- go: Function ;
18
- push: Function ;
19
- replace: Function ;
20
- onInit: Function ;
17
+ go: ( n : number ) => void ;
18
+ push: ( loc : RawLocation ) => void ;
19
+ replace: ( loc : RawLocation ) => void ;
20
+ onInit: ( cb : Function ) => void ;
21
+ getLocation: ( ) => string ;
21
22
22
23
constructor ( router : VueRouter , base : ?string ) {
23
24
this . router = router
24
- this . base = normalizeBae ( base )
25
+ this . base = normalizeBase ( base )
25
26
// start with a route object that stands for "nowhere"
26
27
this . current = createRoute ( null , {
27
28
path : '__vue_router_init__'
@@ -87,13 +88,9 @@ export class History {
87
88
hook && hook ( route )
88
89
} )
89
90
}
90
-
91
- getLocation ( ) : string {
92
- return '/'
93
- }
94
91
}
95
92
96
- function normalizeBae ( base : ?string ) : string {
93
+ function normalizeBase ( base : ?string ) : string {
97
94
if ( ! base ) {
98
95
if ( inBrowser ) {
99
96
// respect <base> tag
Original file line number Diff line number Diff line change @@ -12,13 +12,13 @@ export class HashHistory extends History {
12
12
if ( fallback && this . checkFallback ( ) ) {
13
13
return
14
14
}
15
+ ensureSlash ( )
15
16
window . addEventListener ( 'hashchange' , ( ) => {
16
17
this . onHashChange ( )
17
18
} )
18
19
}
19
20
20
21
onInit ( ) {
21
- ensureSlash ( )
22
22
// possible redirect on start
23
23
if ( getHash ( ) !== this . current . fullPath ) {
24
24
replaceHash ( this . current . fullPath )
Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ export function getLocation (base: string): string {
112
112
if ( base && path . indexOf ( base ) === 0 ) {
113
113
path = path . slice ( base . length )
114
114
}
115
- return path + window . location . search + window . location . hash
115
+ return ( path || '/' ) + window . location . search + window . location . hash
116
116
}
117
117
118
118
function pushState ( url : string , replace ?: boolean ) {
Original file line number Diff line number Diff line change @@ -13,6 +13,22 @@ describe('Location utils', () => {
13
13
} ) )
14
14
} )
15
15
16
+ it ( 'empty string' , function ( ) {
17
+ const loc = normalizeLocation ( '' , { path : '/abc' } )
18
+ expect ( loc . _normalized ) . toBe ( true )
19
+ expect ( loc . path ) . toBe ( '/abc' )
20
+ expect ( loc . hash ) . toBe ( '' )
21
+ expect ( JSON . stringify ( loc . query ) ) . toBe ( JSON . stringify ( { } ) )
22
+ } )
23
+
24
+ it ( 'undefined' , function ( ) {
25
+ const loc = normalizeLocation ( { } , { path : '/abc' } )
26
+ expect ( loc . _normalized ) . toBe ( true )
27
+ expect ( loc . path ) . toBe ( '/abc' )
28
+ expect ( loc . hash ) . toBe ( '' )
29
+ expect ( JSON . stringify ( loc . query ) ) . toBe ( JSON . stringify ( { } ) )
30
+ } )
31
+
16
32
it ( 'relative' , ( ) => {
17
33
const loc = normalizeLocation ( 'abc?foo=bar&baz=qux#hello' , {
18
34
path : '/root/next'
You can’t perform that action at this time.
0 commit comments