Skip to content

Commit c391b85

Browse files
committed
handle query string in redirect
1 parent 3e84d0a commit c391b85

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

src/router/internal.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,17 @@ module.exports = function (Vue, Router) {
9090
var router = this
9191
this._guardRecognizer.add([{
9292
path: path,
93-
handler: function (match) {
94-
var realPath = mappedPath
95-
if (match.isDynamic) {
96-
for (var key in match.params) {
97-
realPath = replaceParam(realPath, match, key)
98-
}
99-
}
93+
handler: function (match, query) {
94+
var realPath = routerUtil.mapParams(
95+
mappedPath,
96+
match.params,
97+
query
98+
)
10099
handler.call(router, realPath)
101100
}
102101
}])
103102
}
104103

105-
/**
106-
* Replace a param segment with real value in a matched
107-
* path.
108-
*
109-
* @param {String} path
110-
* @param {Object} match
111-
* @param {String} key
112-
* @return {String}
113-
*/
114-
115-
function replaceParam (path, match, key) {
116-
var regex = new RegExp(':' + key + '(\\/|$)')
117-
var value = match.params[key]
118-
return path.replace(regex, function (m) {
119-
return m.charAt(m.length - 1) === '/'
120-
? value + '/'
121-
: value
122-
})
123-
}
124-
125104
/**
126105
* Check if a path matches any redirect records.
127106
*
@@ -132,7 +111,7 @@ module.exports = function (Vue, Router) {
132111
p._checkGuard = function (path) {
133112
var matched = this._guardRecognizer.recognize(path)
134113
if (matched) {
135-
matched[0].handler(matched[0])
114+
matched[0].handler(matched[0], matched.queryParams)
136115
return true
137116
}
138117
}

src/util.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
var RouteRecognizer = require('route-recognizer')
2+
var genQuery = RouteRecognizer.prototype.generateQueryString
3+
14
/**
25
* Warn stuff.
36
*
@@ -113,3 +116,41 @@ exports.resolveAsyncComponent = function (handler, cb) {
113116
cb(Component)
114117
})
115118
}
119+
120+
/**
121+
* Map the dynamic segments in a path to params.
122+
*
123+
* @param {String} path
124+
* @param {Object} params
125+
* @param {Object} query
126+
*/
127+
128+
exports.mapParams = function (path, params, query) {
129+
for (var key in params) {
130+
path = replaceParam(path, params, key)
131+
}
132+
if (query) {
133+
path += genQuery(query)
134+
}
135+
return path
136+
}
137+
138+
/**
139+
* Replace a param segment with real value in a matched
140+
* path.
141+
*
142+
* @param {String} path
143+
* @param {Object} params
144+
* @param {String} key
145+
* @return {String}
146+
*/
147+
148+
function replaceParam (path, params, key) {
149+
var regex = new RegExp(':' + key + '(\\/|$)')
150+
var value = params[key]
151+
return path.replace(regex, function (m) {
152+
return m.charAt(m.length - 1) === '/'
153+
? value + '/'
154+
: value
155+
})
156+
}

test/unit/specs/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('Core', function () {
319319
},
320320
'/c': {
321321
component: {
322-
template: 'world'
322+
template: '{{$route.query.msg}}'
323323
}
324324
}
325325
}
@@ -335,7 +335,7 @@ describe('Core', function () {
335335
router.start(App, el)
336336
assertRoutes([
337337
['/whatever', 'hello'],
338-
['/ok', 'world']
338+
['/ok?msg=world', 'world']
339339
], done)
340340
})
341341

0 commit comments

Comments
 (0)