Skip to content

Commit b832e98

Browse files
committed
Merge pull request #426 from tejitak/expose-stringifyPath
expose router.stringifyPath (fix #409)
2 parents bb20060 + cf00b48 commit b832e98

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

src/directives/link.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ export default function (Vue) {
101101
},
102102

103103
onRouteUpdate (route) {
104-
// router._stringifyPath is dependent on current route
104+
// router.stringifyPath is dependent on current route
105105
// and needs to be called again whenver route changes.
106-
var newPath = this.router._stringifyPath(this.target)
106+
var newPath = this.router.stringifyPath(this.target)
107107
if (this.path !== newPath) {
108108
this.path = newPath
109109
this.updateActiveMatch()

src/index.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class Router {
206206
replace = path.replace
207207
append = path.append
208208
}
209-
path = this._stringifyPath(path)
209+
path = this.stringifyPath(path)
210210
if (path) {
211211
this.history.go(path, replace, append)
212212
}
@@ -295,6 +295,47 @@ class Router {
295295
this._started = false
296296
}
297297

298+
/**
299+
* Normalize named route object / string paths into
300+
* a string.
301+
*
302+
* @param {Object|String|Number} path
303+
* @return {String}
304+
*/
305+
306+
stringifyPath (path) {
307+
let fullPath = ''
308+
if (path && typeof path === 'object') {
309+
if (path.name) {
310+
const extend = Vue.util.extend
311+
const currentParams =
312+
this._currentTransition &&
313+
this._currentTransition.to.params
314+
const targetParams = path.params || {}
315+
const params = currentParams
316+
? extend(extend({}, currentParams), targetParams)
317+
: targetParams
318+
if (path.query) {
319+
params.queryParams = path.query
320+
}
321+
fullPath = encodeURI(this._recognizer.generate(path.name, params))
322+
} else if (path.path) {
323+
fullPath = encodeURI(path.path)
324+
if (path.query) {
325+
const query = this._recognizer.generateQueryString(path.query)
326+
if (fullPath.indexOf('?') > -1) {
327+
fullPath += '&' + query.slice(1)
328+
} else {
329+
fullPath += query
330+
}
331+
}
332+
}
333+
} else {
334+
fullPath = encodeURI(path ? path + '' : '')
335+
}
336+
return fullPath
337+
}
338+
298339
// Internal methods ======================================
299340

300341
/**
@@ -558,47 +599,6 @@ class Router {
558599
})
559600
}
560601
}
561-
562-
/**
563-
* Normalize named route object / string paths into
564-
* a string.
565-
*
566-
* @param {Object|String|Number} path
567-
* @return {String}
568-
*/
569-
570-
_stringifyPath (path) {
571-
let fullPath = ''
572-
if (path && typeof path === 'object') {
573-
if (path.name) {
574-
const extend = Vue.util.extend
575-
const currentParams =
576-
this._currentTransition &&
577-
this._currentTransition.to.params
578-
const targetParams = path.params || {}
579-
const params = currentParams
580-
? extend(extend({}, currentParams), targetParams)
581-
: targetParams
582-
if (path.query) {
583-
params.queryParams = path.query
584-
}
585-
fullPath = encodeURI(this._recognizer.generate(path.name, params))
586-
} else if (path.path) {
587-
fullPath = encodeURI(path.path)
588-
if (path.query) {
589-
const query = this._recognizer.generateQueryString(path.query)
590-
if (fullPath.indexOf('?') > -1) {
591-
fullPath += '&' + query.slice(1)
592-
} else {
593-
fullPath += query
594-
}
595-
}
596-
}
597-
} else {
598-
fullPath = encodeURI(path ? path + '' : '')
599-
}
600-
return fullPath
601-
}
602602
}
603603

604604
/**

test/unit/specs/core.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,14 +1117,14 @@ describe('Stringify Path', function () {
11171117
})
11181118

11191119
it('plain string', function () {
1120-
expect(router._stringifyPath('a')).toBe('a')
1120+
expect(router.stringifyPath('a')).toBe('a')
11211121
})
11221122

11231123
it('object path', function () {
1124-
expect(router._stringifyPath({ path: '/hi' })).toBe('/hi')
1125-
expect(router._stringifyPath({ path: '/hi', query: { a: 1 } })).toBe('/hi?a=1')
1126-
expect(router._stringifyPath({ path: '/hi', query: { a: 1, b: 2 } })).toBe('/hi?a=1&b=2')
1127-
expect(router._stringifyPath({ path: '/hi?c=3', query: { a: 1, b: 2 } })).toBe('/hi?c=3&a=1&b=2')
1124+
expect(router.stringifyPath({ path: '/hi' })).toBe('/hi')
1125+
expect(router.stringifyPath({ path: '/hi', query: { a: 1 } })).toBe('/hi?a=1')
1126+
expect(router.stringifyPath({ path: '/hi', query: { a: 1, b: 2 } })).toBe('/hi?a=1&b=2')
1127+
expect(router.stringifyPath({ path: '/hi?c=3', query: { a: 1, b: 2 } })).toBe('/hi?c=3&a=1&b=2')
11281128
})
11291129

11301130
it('named route', function () {
@@ -1134,14 +1134,14 @@ describe('Stringify Path', function () {
11341134
component: {}
11351135
}
11361136
})
1137-
expect(router._stringifyPath({ name: 'a' })).toBe('/test/:id')
1138-
expect(router._stringifyPath({ name: 'a', params: { id: 0 } })).toBe('/test/0')
1139-
expect(router._stringifyPath({ name: 'a', params: { id: 'hi' } })).toBe('/test/hi')
1137+
expect(router.stringifyPath({ name: 'a' })).toBe('/test/:id')
1138+
expect(router.stringifyPath({ name: 'a', params: { id: 0 } })).toBe('/test/0')
1139+
expect(router.stringifyPath({ name: 'a', params: { id: 'hi' } })).toBe('/test/hi')
11401140
})
11411141

11421142
it('named route not found should throw error', function () {
11431143
expect(function () {
1144-
router._stringifyPath({
1144+
router.stringifyPath({
11451145
name: 'a'
11461146
})
11471147
}).toThrow()
@@ -1154,9 +1154,9 @@ describe('Stringify Path', function () {
11541154
component: {}
11551155
}
11561156
})
1157-
expect(router._stringifyPath('/hi/你好')).toBe(encodeURI('/hi/你好'))
1158-
expect(router._stringifyPath({ path: '/hi/你好' })).toBe(encodeURI('/hi/你好'))
1159-
expect(router._stringifyPath({ name: 'a', params: { id: '你好' }})).toBe(encodeURI('/test/你好'))
1157+
expect(router.stringifyPath('/hi/你好')).toBe(encodeURI('/hi/你好'))
1158+
expect(router.stringifyPath({ path: '/hi/你好' })).toBe(encodeURI('/hi/你好'))
1159+
expect(router.stringifyPath({ name: 'a', params: { id: '你好' }})).toBe(encodeURI('/test/你好'))
11601160
})
11611161

11621162
})

0 commit comments

Comments
 (0)