Skip to content

Commit

Permalink
Add public Router#resolve method as per #831. (#918)
Browse files Browse the repository at this point in the history
Necessary docs, tests, typings. Fixes per code review.
  • Loading branch information
hogart authored and yyx990803 committed Nov 23, 2016
1 parent 139803d commit 38e199d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docs/en/api/router-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
- **router.getMatchedComponents()**

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.

- **router.resolve(location, current?, append?)**

Reverse URL resolving. Given location in form same as used in `<router-link/>`, returns object with string property `href`.
19 changes: 4 additions & 15 deletions src/components/link.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* @flow */

import { cleanPath } from '../util/path'
import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
import { normalizeLocation } from '../util/location'
import { _Vue } from '../install'

// work around weird flow bug
Expand All @@ -27,14 +25,10 @@ export default {
render (h: Function) {
const router = this.$router
const current = this.$route
const to = normalizeLocation(this.to, current, this.append)
const resolved = router.match(to, current)
const fullPath = resolved.redirectedFrom || resolved.fullPath
const base = router.history.base
const href = createHref(base, fullPath, router.mode)
const { normalizedTo, resolved, href } = router.resolve(this.to, current, this.append)
const classes = {}
const activeClass = this.activeClass || router.options.linkActiveClass || 'router-link-active'
const compareTarget = to.path ? createRoute(null, to) : resolved
const compareTarget = normalizedTo.path ? createRoute(null, normalizedTo) : resolved
classes[activeClass] = this.exact
? isSameRoute(current, compareTarget)
: isIncludedRoute(current, compareTarget)
Expand All @@ -57,9 +51,9 @@ export default {

e.preventDefault()
if (this.replace) {
router.replace(to)
router.replace(normalizedTo)
} else {
router.push(to)
router.push(normalizedTo)
}
}
}
Expand Down Expand Up @@ -106,8 +100,3 @@ function findAnchor (children) {
}
}
}

function createHref (base, fullPath, mode) {
var path = mode === 'hash' ? '/#' + fullPath : fullPath
return base ? cleanPath(base + path) : path
}
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { HTML5History, getLocation } from './history/html5'
import { AbstractHistory } from './history/abstract'
import { inBrowser, supportsHistory } from './util/dom'
import { assert } from './util/warn'
import { cleanPath } from './util/path'
import { normalizeLocation } from './util/location'

export default class VueRouter {
static install: () => void;
Expand Down Expand Up @@ -120,6 +122,24 @@ export default class VueRouter {
})
}))
}

resolve (to: RawLocation, current?: Route, append?: boolean): {href: string} {
const normalizedTo = normalizeLocation(to, current || this.history.current, append)
const resolved = this.match(normalizedTo, current)
const fullPath = resolved.redirectedFrom || resolved.fullPath
const base = this.history.base
const href = createHref(base, fullPath, this.mode)
return {
normalizedTo,
resolved,
href
}
}
}

function createHref (base: string, fullPath: string, mode) {
var path = mode === 'hash' ? '/#' + fullPath : fullPath
return base ? cleanPath(base + path) : path
}

VueRouter.install = install
Expand Down
1 change: 1 addition & 0 deletions types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare class VueRouter {
back (): void;
forward (): void;
getMatchedComponentes (): Component;
resolve (to: RawLocation, current?: Route, append?: boolean): {href: string};

static install: PluginFunction<never>;
}
Expand Down

0 comments on commit 38e199d

Please sign in to comment.