@@ -24,7 +24,12 @@ import {
24
24
} from './utils/scroll'
25
25
import { createRouterMatcher } from './matcher'
26
26
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'
28
33
import { useCallbacks } from './utils/callbacks'
29
34
import { encodeParam , decode } from './utils/encoding'
30
35
import {
@@ -102,8 +107,6 @@ export interface Router {
102
107
install ( app : App ) : void
103
108
}
104
109
105
- const isClient = typeof window !== 'undefined'
106
-
107
110
export function createRouter ( {
108
111
history,
109
112
routes,
@@ -120,7 +123,7 @@ export function createRouter({
120
123
)
121
124
let pendingLocation : RouteLocation = START_LOCATION_NORMALIZED
122
125
123
- if ( isClient && 'scrollRestoration' in window . history ) {
126
+ if ( isBrowser && 'scrollRestoration' in window . history ) {
124
127
window . history . scrollRestoration = 'manual'
125
128
}
126
129
@@ -422,7 +425,7 @@ export function createRouter({
422
425
currentRoute . value = markNonReactive ( toLocation )
423
426
// TODO: this doesn't work on first load. Moving it to RouterView could allow automatically handling transitions too maybe
424
427
// TODO: refactor with a state getter
425
- const state = isPush || ! isClient ? { } : window . history . state
428
+ const state = isPush || ! isBrowser ? { } : window . history . state
426
429
const savedScroll = getSavedScroll ( getScrollKey ( toLocation . fullPath , 0 ) )
427
430
handleScroll (
428
431
toLocation ,
@@ -576,22 +579,31 @@ function applyRouterPlugin(app: App, router: Router) {
576
579
app . component ( 'RouterView' , View )
577
580
578
581
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
+ } )
595
607
596
608
const reactiveRoute = { } as {
597
609
[ k in keyof RouteLocationNormalizedLoaded ] : ComputedRef <
0 commit comments