File tree 3 files changed +33
-3
lines changed 3 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,14 @@ import {
5
5
createWebHistory ,
6
6
useRoute ,
7
7
loadRouteLocation ,
8
+ useHistoryState ,
8
9
} from 'vue-router'
9
10
import {
10
11
createApp ,
11
12
readonly ,
12
13
ref ,
14
+ watch ,
15
+ shallowRef ,
13
16
watchEffect ,
14
17
computed ,
15
18
defineComponent ,
@@ -72,6 +75,7 @@ const Home = defineComponent({
72
75
const route = useRoute ( )
73
76
74
77
const userId = computed ( ( ) => route . params . id )
78
+ const historyState = useHistoryState < { backgroundView ?: string } > ( )
75
79
76
80
watchEffect (
77
81
( ) => {
@@ -187,14 +191,17 @@ router.beforeEach(to => {
187
191
const app = createApp ( {
188
192
setup ( ) {
189
193
const route = useRoute ( )
194
+ const historyState = useHistoryState < { backgroundView ?: string } > ( )
190
195
191
- const routeWithModal = computed ( ( ) => {
196
+ const routeWithModal = shallowRef < RouteLocationNormalizedLoaded > ( route )
197
+ watch ( historyState , async ( ) => {
192
198
if ( historyState . value . backgroundView ) {
193
- return router . resolve (
199
+ routeWithModal . value = router . resolve (
194
200
historyState . value . backgroundView
195
201
) as RouteLocationNormalizedLoaded
202
+ await loadRouteLocation ( routeWithModal . value )
196
203
} else {
197
- return route
204
+ routeWithModal . value = route
198
205
}
199
206
} )
200
207
Original file line number Diff line number Diff line change
1
+ import { isBrowser } from '../utils'
2
+ import { useRouter } from '../useApi'
3
+ import { computed } from 'vue'
4
+ import { HistoryState } from './common'
5
+
6
+ /**
7
+ * Reactive history state. Only available in browser.
8
+ *
9
+ * @experimental - DO NOT use in production
10
+ */
11
+ export function useHistoryState < T = HistoryState > ( ) {
12
+ const router = useRouter ( )
13
+ return computed < Readonly < T > > ( ( ) => {
14
+ if ( ! isBrowser ) {
15
+ return { }
16
+ }
17
+
18
+ // Enforce automatically update when navigation happens
19
+ router . currentRoute . value
20
+ return window . history . state
21
+ } )
22
+ }
Original file line number Diff line number Diff line change 1
1
export { createWebHistory } from './history/html5'
2
+ export { useHistoryState } from './history/state'
2
3
export { createMemoryHistory } from './history/memory'
3
4
export { createWebHashHistory } from './history/hash'
4
5
export { createRouterMatcher } from './matcher'
You can’t perform that action at this time.
0 commit comments