@@ -28,38 +28,49 @@ export class HashHistory extends History {
28
28
setupScroll ( )
29
29
}
30
30
31
- window . addEventListener ( supportsPushState ? 'popstate' : 'hashchange' , ( ) => {
32
- const current = this . current
33
- if ( ! ensureSlash ( ) ) {
34
- return
35
- }
36
- this . transitionTo ( getHash ( ) , route => {
37
- if ( supportsScroll ) {
38
- handleScroll ( this . router , route , current , true )
39
- }
40
- if ( ! supportsPushState ) {
41
- replaceHash ( route . fullPath )
31
+ window . addEventListener (
32
+ supportsPushState ? 'popstate' : 'hashchange' ,
33
+ ( ) => {
34
+ const current = this . current
35
+ if ( ! ensureSlash ( ) ) {
36
+ return
42
37
}
43
- } )
44
- } )
38
+ this . transitionTo ( getHash ( ) , route => {
39
+ if ( supportsScroll ) {
40
+ handleScroll ( this . router , route , current , true )
41
+ }
42
+ if ( ! supportsPushState ) {
43
+ replaceHash ( route . fullPath )
44
+ }
45
+ } )
46
+ }
47
+ )
45
48
}
46
49
47
50
push ( location : RawLocation , onComplete ?: Function , onAbort ?: Function ) {
48
51
const { current : fromRoute } = this
49
- this . transitionTo ( location , route => {
50
- pushHash ( route . fullPath )
51
- handleScroll ( this . router , route , fromRoute , false )
52
- onComplete && onComplete ( route )
53
- } , onAbort )
52
+ this . transitionTo (
53
+ location ,
54
+ route => {
55
+ pushHash ( route . fullPath )
56
+ handleScroll ( this . router , route , fromRoute , false )
57
+ onComplete && onComplete ( route )
58
+ } ,
59
+ onAbort
60
+ )
54
61
}
55
62
56
63
replace ( location : RawLocation , onComplete ? : Function , onAbort ? : Function ) {
57
64
const { current : fromRoute } = this
58
- this . transitionTo ( location , route => {
59
- replaceHash ( route . fullPath )
60
- handleScroll ( this . router , route , fromRoute , false )
61
- onComplete && onComplete ( route )
62
- } , onAbort )
65
+ this . transitionTo (
66
+ location ,
67
+ route => {
68
+ replaceHash ( route . fullPath )
69
+ handleScroll ( this . router , route , fromRoute , false )
70
+ onComplete && onComplete ( route )
71
+ } ,
72
+ onAbort
73
+ )
63
74
}
64
75
65
76
go ( n : number ) {
@@ -81,9 +92,7 @@ export class HashHistory extends History {
81
92
function checkFallback ( base ) {
82
93
const location = getLocation ( base )
83
94
if ( ! / ^ \/ # / . test ( location ) ) {
84
- window . location . replace (
85
- cleanPath ( base + '/#' + location )
86
- )
95
+ window . location . replace ( cleanPath ( base + '/#' + location ) )
87
96
return true
88
97
}
89
98
}
@@ -112,20 +121,28 @@ export function getHash (): string {
112
121
const searchIndex = href . indexOf ( '?' )
113
122
if ( searchIndex < 0 ) {
114
123
const hashIndex = href . indexOf ( '#' )
115
- if ( hashIndex > - 1 ) href = decodeURI ( href . slice ( 0 , hashIndex ) ) + href . slice ( hashIndex )
116
- else href = decodeURI ( href )
124
+ if ( hashIndex > - 1 ) {
125
+ href = decodeURI ( href . slice ( 0 , hashIndex ) ) + href . slice ( hashIndex )
126
+ } else href = decodeURI ( href )
117
127
} else {
118
- if ( searchIndex > - 1 ) href = decodeURI ( href . slice ( 0 , searchIndex ) ) + href . slice ( searchIndex )
128
+ if ( searchIndex > - 1 ) {
129
+ href = decodeURI ( href . slice ( 0 , searchIndex ) ) + href . slice ( searchIndex )
130
+ }
119
131
}
120
132
121
133
return href
122
134
}
123
135
124
136
function getUrl ( path ) {
125
137
const href = window . location . href
126
- const i = href . indexOf ( '#' )
127
- const base = i >= 0 ? href . slice ( 0 , i ) : href
128
- return `${ base } #${ path } `
138
+ const hashPos = href . indexOf ( '#' )
139
+ let base = hashPos > - 1 ? href . slice ( 0 , hashPos ) : href
140
+
141
+ const searchPos = base . indexOf ( '?' )
142
+ const query = searchPos > - 1 ? base . slice ( searchPos ) : ''
143
+ base = query ? base . slice ( 0 , searchPos ) : base
144
+
145
+ return `${ base } #${ path + query } `
129
146
}
130
147
131
148
function pushHash ( path ) {
0 commit comments