|
1 | | -import { |
2 | | - getCurrentInstance, |
3 | | - shallowReactive, |
4 | | - effectScope, |
5 | | - onUnmounted |
6 | | -} from 'vue' |
7 | | - |
8 | | -export function useRouter () { |
9 | | - const i = getCurrentInstance() |
10 | | - if (process.env.NODE_ENV !== 'production' && !i) { |
11 | | - throwNoCurrentInstance('useRouter') |
12 | | - } |
13 | | - |
14 | | - return i.proxy.$root.$router |
15 | | -} |
16 | | - |
17 | | -export function useRoute () { |
18 | | - const i = getCurrentInstance() |
19 | | - if (process.env.NODE_ENV !== 'production' && !i) { |
20 | | - throwNoCurrentInstance('useRoute') |
21 | | - } |
22 | | - |
23 | | - const root = i.proxy.$root |
24 | | - if (!root._$route) { |
25 | | - const route = effectScope(true).run(() => |
26 | | - shallowReactive(Object.assign({}, root.$router.currentRoute)) |
27 | | - ) |
28 | | - root._$route = route |
29 | | - |
30 | | - root.$router.afterEach(to => { |
31 | | - Object.assign(route, to) |
32 | | - }) |
33 | | - } |
34 | | - |
35 | | - return root._$route |
36 | | -} |
37 | | - |
38 | | -// TODO: |
39 | | -// export function useLink () {} |
40 | | - |
41 | | -export function onBeforeRouteUpdate (guard) { |
42 | | - const i = getCurrentInstance() |
43 | | - if (process.env.NODE_ENV !== 'production' && !i) { |
44 | | - throwNoCurrentInstance('onBeforeRouteUpdate') |
45 | | - } |
46 | | - |
47 | | - return useFilteredGuard(guard, isUpdateNavigation) |
48 | | -} |
49 | | - |
50 | | -function useFilteredGuard (guard, fn) { |
51 | | - const i = getCurrentInstance() |
52 | | - const router = useRouter() |
53 | | - |
54 | | - let target = i.proxy |
55 | | - // find the nearest RouterView to know the depth |
56 | | - while ( |
57 | | - target && |
58 | | - target.$vnode && |
59 | | - target.$vnode.data && |
60 | | - target.$vnode.data.routerViewDepth == null |
61 | | - ) { |
62 | | - target = target.$parent |
63 | | - } |
64 | | - |
65 | | - const depth = |
66 | | - target && target.$vnode && target.$vnode.data |
67 | | - ? target.$vnode.data.routerViewDepth |
68 | | - : null |
69 | | - |
70 | | - if (depth != null) { |
71 | | - const removeGuard = router.beforeEach((to, from, next) => { |
72 | | - return fn(to, from, depth) ? guard(to, from, next) : next() |
73 | | - }) |
74 | | - |
75 | | - onUnmounted(removeGuard) |
76 | | - return removeGuard |
77 | | - } |
78 | | - |
79 | | - return noop |
80 | | -} |
81 | | - |
82 | | -function isUpdateNavigation (to, from, depth) { |
83 | | - const toMatched = to.matched |
84 | | - const fromMatched = from.matched |
85 | | - return ( |
86 | | - toMatched.length >= depth && |
87 | | - toMatched |
88 | | - .slice(0, depth + 1) |
89 | | - .every((record, i) => record === fromMatched[i]) |
90 | | - ) |
91 | | -} |
92 | | - |
93 | | -function isLeaveNavigation (to, from, depth) { |
94 | | - const toMatched = to.matched |
95 | | - const fromMatched = from.matched |
96 | | - return toMatched.length < depth || toMatched[depth] !== fromMatched[depth] |
97 | | -} |
98 | | - |
99 | | -const noop = () => {} |
100 | | - |
101 | | -export function onBeforeRouteLeave (guard) { |
102 | | - const i = getCurrentInstance() |
103 | | - if (process.env.NODE_ENV !== 'production' && !i) { |
104 | | - throwNoCurrentInstance('onBeforeRouteLeave') |
105 | | - } |
106 | | - |
107 | | - return useFilteredGuard(guard, isLeaveNavigation) |
108 | | -} |
109 | | - |
110 | | -function throwNoCurrentInstance (method) { |
111 | | - throw new Error( |
112 | | - `[vue-router]: Missing current instance. ${method}() must be called inside <script setup> or setup().` |
113 | | - ) |
114 | | -} |
| 1 | +export * from './guards' |
| 2 | +export * from './globals' |
| 3 | +export * from './useLink' |
0 commit comments