Resolution for you:
// replace:// const userToLoad = computed(() => root.$route.params.userId);// to: import { useRoute } from '@/router/migrateRouterVue3';//...const route = useRoute();const userToLoad = computed(() => route.params.userId);
You can pass the parameters as props to your components. Props are reactive by default.
This is how the route configuration could look like:
{path: '/users/:userId',name: Users,component: YourComponent},
You can then use the props in your component with watchEffect()
<template><div>{{username}}</div></template><script lang="ts">import { computed, defineComponent, ref, getCurrentInstance, watchEffect } from '@vue/composition-api';export const useUsername = ({ user }) => {return { username: user.name };};export default defineComponent({props: {userId: {type: String, required: true },setup(props, { root }) {const vm = getCurrentInstance();const user = ref()const userToLoad = computed(() => props.userId);const listOfUsers = [{ userId: 1, name: 'user1' },{ userId: 2, name: 'user2' },];watchEffect(() => user.value = listOfUsers.find((u) => u.userId === +userToLoad.value))if (!user) {return root.$router.push('/404');}const { username } = useUsername({ user });return { username };},});</script>
watchEffect()
will run immediately when defined and when reactive dependencies.change
function useRoute() {const vm = getCurrentInstance()if (!vm) throw new Error('must be called in setup')return vm.proxy.$route}
https://github.com/vuejs/composition-api/issues/630
The following useRoute
hook will make route
reactive so that it's doable:
const route = useRoute();const fooId = computed(()=>route.params.fooId);
let currentRoute = null;export const useRoute = () => {const self = getCurrentInstance(); const router = self.proxy.$router;if (!currentRoute) {const route = { ...self.proxy.$route };const routeRef = shallowRef(route);const computedRoute = {};for (const key of Object.keys(routeRef.value)) {computedRoute[key] = computed(() => routeRef.value[key]);}router.afterEach((to) => {routeRef.value = to;});currentRoute = reactive(computedRoute);}return currentRoute;};
The vue2-helpers
package provides a useRoute
function you can use in Vue 2.7 (and 2.6, 2.5 also).
# Vue 2.7$ npm install vue2-helpers@2# Vue 2.5 and 2.6$ npm install vue2-helpers@1
import { useRoute } from 'vue2-helpers/vue-router';const route = useRoute();const id: string | undefined = route.params.id;
const { proxy } = getCurrentInstance();
then use proxy to access $router
or $route
Add please this code: watchEffect(() => userToLoad);