Skip to content

Commit

Permalink
Use isRouteLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code committed Mar 5, 2024
1 parent 0598e88 commit f2a1a56
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/router/src/RouterLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ComponentOptionsMixin,
} from 'vue'
import {
isRouteLocation,
RouteLocationRaw,
VueUseOptions,
RouteLocation,
Expand All @@ -36,7 +37,7 @@ import { isSameRouteLocationParams, isSameRouteRecord } from './location'
import { routerKey, routeLocationKey } from './injectionSymbols'
import { RouteRecord } from './matcher/types'
import { NavigationFailure } from './errors'
import { isArray, isBrowser, isObject, noop } from './utils'
import { isArray, isBrowser, noop } from './utils'
import { warn } from './warning'

export interface RouterLinkOptions {
Expand Down Expand Up @@ -95,15 +96,14 @@ export function useLink(props: UseLinkOptions) {
const router = inject(routerKey)!
const currentRoute = inject(routeLocationKey)!

const isValidTo = (to: unknown) => typeof to === 'string' || isObject(to)
let hasPrevious = false
let previousTo: unknown = null

const route = computed(() => {
const to = unref(props.to)

if (__DEV__ && (!hasPrevious || to !== previousTo)) {
if (!isValidTo(to)) {
if (!isRouteLocation(to)) {
if (hasPrevious) {
warn(
`Invalid value for prop "to" in useLink()\n- to:`,
Expand Down Expand Up @@ -204,7 +204,7 @@ export function useLink(props: UseLinkOptions) {
linkContextDevtools.route = route.value
linkContextDevtools.isActive = isActive.value
linkContextDevtools.isExactActive = isExactActive.value
linkContextDevtools.error = isValidTo(unref(props.to))
linkContextDevtools.error = isRouteLocation(unref(props.to))
? null
: 'Invalid "to" value'
},
Expand Down
12 changes: 3 additions & 9 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RouteLocationNormalizedLoaded,
RouteLocation,
RouteRecordName,
isRouteLocation,
isRouteName,
NavigationGuardWithThis,
RouteLocationOptions,
Expand All @@ -32,14 +33,7 @@ import {
NavigationRedirectError,
isNavigationFailure,
} from './errors'
import {
applyToParams,
isBrowser,
assign,
noop,
isArray,
isObject,
} from './utils'
import { applyToParams, isBrowser, assign, noop, isArray } from './utils'
import { useCallbacks } from './utils/callbacks'
import { encodeParam, decode, encodeHash } from './encoding'
import {
Expand Down Expand Up @@ -467,7 +461,7 @@ export function createRouter(options: RouterOptions): Router {
})
}

if (__DEV__ && !isObject(rawLocation)) {
if (__DEV__ && !isRouteLocation(rawLocation)) {
warn(
`router.resolve() was passed an invalid location. This will fail in production.\n- Location:`,
rawLocation
Expand Down

0 comments on commit f2a1a56

Please sign in to comment.