Skip to content

Commit 1807f30

Browse files
committedApr 16, 2020
feat: add this.$router
1 parent 753306c commit 1807f30

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed
 

‎src/router.ts

+33-21
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import {
2424
} from './utils/scroll'
2525
import { createRouterMatcher } from './matcher'
2626
import { createRouterError, ErrorTypes, NavigationError } from './errors'
27-
import { applyToParams, isSameRouteRecord, isSameLocationObject } from './utils'
27+
import {
28+
applyToParams,
29+
isSameRouteRecord,
30+
isSameLocationObject,
31+
isBrowser,
32+
} from './utils'
2833
import { useCallbacks } from './utils/callbacks'
2934
import { encodeParam, decode } from './utils/encoding'
3035
import {
@@ -102,8 +107,6 @@ export interface Router {
102107
install(app: App): void
103108
}
104109

105-
const isClient = typeof window !== 'undefined'
106-
107110
export function createRouter({
108111
history,
109112
routes,
@@ -120,7 +123,7 @@ export function createRouter({
120123
)
121124
let pendingLocation: RouteLocation = START_LOCATION_NORMALIZED
122125

123-
if (isClient && 'scrollRestoration' in window.history) {
126+
if (isBrowser && 'scrollRestoration' in window.history) {
124127
window.history.scrollRestoration = 'manual'
125128
}
126129

@@ -422,7 +425,7 @@ export function createRouter({
422425
currentRoute.value = markNonReactive(toLocation)
423426
// TODO: this doesn't work on first load. Moving it to RouterView could allow automatically handling transitions too maybe
424427
// TODO: refactor with a state getter
425-
const state = isPush || !isClient ? {} : window.history.state
428+
const state = isPush || !isBrowser ? {} : window.history.state
426429
const savedScroll = getSavedScroll(getScrollKey(toLocation.fullPath, 0))
427430
handleScroll(
428431
toLocation,
@@ -576,22 +579,31 @@ function applyRouterPlugin(app: App, router: Router) {
576579
app.component('RouterView', View)
577580

578581
let started = false
579-
// TODO: can we use something that isn't a mixin?
580-
// TODO: this initial navigation is only necessary on client, on server it doesn't make sense
581-
// because it will create an extra unnecessary navigation and could lead to problems
582-
if (isClient)
583-
app.mixin({
584-
beforeCreate() {
585-
if (!started) {
586-
router.push(router.history.location.fullPath).catch(err => {
587-
if (__DEV__)
588-
console.error('Unhandled error when starting the router', err)
589-
else return err
590-
})
591-
started = true
592-
}
593-
},
594-
})
582+
// TODO: can we use something that isn't a mixin? Like adding an onMount hook here
583+
app.mixin({
584+
beforeCreate() {
585+
// TODO: add tests
586+
this.$router = this.$parent ? this.$parent.$router : router
587+
// TODO: find a way to allow $route without having to write $route.value
588+
// this.$route = this.$parent ? this.$parent.$route : this.route
589+
590+
// this doesn't work because `this` is a Proxy
591+
// Object.defineProperty(this, '$route', {
592+
// get: () => router.currentRoute.value,
593+
// })
594+
595+
if (isBrowser && !started) {
596+
// this initial navigation is only necessary on client, on server it doesn't make sense
597+
// because it will create an extra unnecessary navigation and could lead to problems
598+
router.push(router.history.location.fullPath).catch(err => {
599+
if (__DEV__)
600+
console.error('Unhandled error when starting the router', err)
601+
else return err
602+
})
603+
started = true
604+
}
605+
},
606+
})
595607

596608
const reactiveRoute = {} as {
597609
[k in keyof RouteLocationNormalizedLoaded]: ComputedRef<

0 commit comments

Comments
 (0)