@@ -28,38 +28,49 @@ export class HashHistory extends History {
2828 setupScroll ( )
2929 }
3030
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
4237 }
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+ )
4548 }
4649
4750 push ( location : RawLocation , onComplete ?: Function , onAbort ?: Function ) {
4851 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+ )
5461 }
5562
5663 replace ( location : RawLocation , onComplete ? : Function , onAbort ? : Function ) {
5764 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+ )
6374 }
6475
6576 go ( n : number ) {
@@ -81,9 +92,7 @@ export class HashHistory extends History {
8192function checkFallback ( base ) {
8293 const location = getLocation ( base )
8394 if ( ! / ^ \/ # / . test ( location ) ) {
84- window . location . replace (
85- cleanPath ( base + '/#' + location )
86- )
95+ window . location . replace ( cleanPath ( base + '/#' + location ) )
8796 return true
8897 }
8998}
@@ -112,20 +121,28 @@ export function getHash (): string {
112121 const searchIndex = href . indexOf ( '?' )
113122 if ( searchIndex < 0 ) {
114123 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 )
117127 } 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+ }
119131 }
120132
121133 return href
122134}
123135
124136function getUrl ( path ) {
125137 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 } `
129146}
130147
131148function pushHash ( path ) {
0 commit comments