Skip to content

Commit 38e199d

Browse files
hogartyyx990803
authored andcommitted
Add public Router#resolve method as per #831. (#918)
Necessary docs, tests, typings. Fixes per code review.
1 parent 139803d commit 38e199d

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

docs/en/api/router-instance.md

+4
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@
3939
- **router.getMatchedComponents()**
4040

4141
Returns an Array of the components (definition/constructor, not instances) matched by the current route. This is mostly used during server-side rendering to perform data prefetching.
42+
43+
- **router.resolve(location, current?, append?)**
44+
45+
Reverse URL resolving. Given location in form same as used in `<router-link/>`, returns object with string property `href`.

src/components/link.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* @flow */
22

3-
import { cleanPath } from '../util/path'
43
import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
5-
import { normalizeLocation } from '../util/location'
64
import { _Vue } from '../install'
75

86
// work around weird flow bug
@@ -27,14 +25,10 @@ export default {
2725
render (h: Function) {
2826
const router = this.$router
2927
const current = this.$route
30-
const to = normalizeLocation(this.to, current, this.append)
31-
const resolved = router.match(to, current)
32-
const fullPath = resolved.redirectedFrom || resolved.fullPath
33-
const base = router.history.base
34-
const href = createHref(base, fullPath, router.mode)
28+
const { normalizedTo, resolved, href } = router.resolve(this.to, current, this.append)
3529
const classes = {}
3630
const activeClass = this.activeClass || router.options.linkActiveClass || 'router-link-active'
37-
const compareTarget = to.path ? createRoute(null, to) : resolved
31+
const compareTarget = normalizedTo.path ? createRoute(null, normalizedTo) : resolved
3832
classes[activeClass] = this.exact
3933
? isSameRoute(current, compareTarget)
4034
: isIncludedRoute(current, compareTarget)
@@ -57,9 +51,9 @@ export default {
5751

5852
e.preventDefault()
5953
if (this.replace) {
60-
router.replace(to)
54+
router.replace(normalizedTo)
6155
} else {
62-
router.push(to)
56+
router.push(normalizedTo)
6357
}
6458
}
6559
}
@@ -106,8 +100,3 @@ function findAnchor (children) {
106100
}
107101
}
108102
}
109-
110-
function createHref (base, fullPath, mode) {
111-
var path = mode === 'hash' ? '/#' + fullPath : fullPath
112-
return base ? cleanPath(base + path) : path
113-
}

src/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { HTML5History, getLocation } from './history/html5'
77
import { AbstractHistory } from './history/abstract'
88
import { inBrowser, supportsHistory } from './util/dom'
99
import { assert } from './util/warn'
10+
import { cleanPath } from './util/path'
11+
import { normalizeLocation } from './util/location'
1012

1113
export default class VueRouter {
1214
static install: () => void;
@@ -120,6 +122,24 @@ export default class VueRouter {
120122
})
121123
}))
122124
}
125+
126+
resolve (to: RawLocation, current?: Route, append?: boolean): {href: string} {
127+
const normalizedTo = normalizeLocation(to, current || this.history.current, append)
128+
const resolved = this.match(normalizedTo, current)
129+
const fullPath = resolved.redirectedFrom || resolved.fullPath
130+
const base = this.history.base
131+
const href = createHref(base, fullPath, this.mode)
132+
return {
133+
normalizedTo,
134+
resolved,
135+
href
136+
}
137+
}
138+
}
139+
140+
function createHref (base: string, fullPath: string, mode) {
141+
var path = mode === 'hash' ? '/#' + fullPath : fullPath
142+
return base ? cleanPath(base + path) : path
123143
}
124144

125145
VueRouter.install = install

types/router.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare class VueRouter {
2828
back (): void;
2929
forward (): void;
3030
getMatchedComponentes (): Component;
31+
resolve (to: RawLocation, current?: Route, append?: boolean): {href: string};
3132

3233
static install: PluginFunction<never>;
3334
}

0 commit comments

Comments
 (0)