@@ -99,22 +99,24 @@ p.redirect = function (map) {
99
99
*/
100
100
101
101
p . go = function ( path , options ) {
102
+ var replace = options && options . replace
102
103
if ( this . _pushstate ) {
103
104
// make it relative to root
104
105
path = this . _root
105
106
? this . _root + '/' + path . replace ( / ^ \/ / , '' )
106
107
: path
107
- if ( options && options . replace ) {
108
+ if ( replace ) {
108
109
history . replaceState ( { } , '' , path )
109
110
} else {
110
111
history . pushState ( { } , '' , path )
111
112
}
112
113
this . _match ( path )
113
114
} else {
114
115
path = path . replace ( / ^ # ! ? / , '' )
115
- location . hash = this . _hashbang
116
+ var hash = this . _hashbang
116
117
? '!' + path
117
118
: path
119
+ setHash ( hash , replace )
118
120
}
119
121
}
120
122
@@ -150,15 +152,16 @@ p.initHashMode = function () {
150
152
var self = this
151
153
this . onRouteChange = function ( ) {
152
154
// format hashbang
153
- if (
154
- self . _hashbang &&
155
- location . hash &&
156
- location . hash . charAt ( 1 ) !== '!'
157
- ) {
158
- location . hash = '!' + location . hash . slice ( 1 )
155
+ var hash = location . hash
156
+ if ( self . _hashbang && hash && hash . charAt ( 1 ) !== '!' ) {
157
+ setHash ( '!' + hash . slice ( 1 ) , true )
159
158
return
160
159
}
161
- var hash = location . hash . replace ( / ^ # ! ? / , '' )
160
+ if ( ! self . _hashbang && hash && hash . charAt ( 1 ) === '!' ) {
161
+ setHash ( hash . slice ( 1 ) , true )
162
+ return
163
+ }
164
+ hash = hash . replace ( / ^ # ! ? / , '' )
162
165
var url = hash + location . search
163
166
url = decodeURI ( url )
164
167
self . _match ( url )
@@ -282,4 +285,20 @@ VueRouter.install = function (Vue) {
282
285
require ( './link' ) ( Vue )
283
286
}
284
287
288
+ /**
289
+ * Set current hash
290
+ *
291
+ * @param {String } hash
292
+ * @param {Boolean } replace
293
+ */
294
+ function setHash ( hash , replace ) {
295
+ if ( replace ) {
296
+ var urlLength = location . href . length - location . hash . length
297
+ var fullURL = location . href . slice ( 0 , urlLength ) + '#' + hash
298
+ location . replace ( fullURL )
299
+ } else {
300
+ location . hash = hash
301
+ }
302
+ }
303
+
285
304
module . exports = VueRouter
0 commit comments