0

I am quite new to Vue in general, especially Vue3. I have a route /mypage that takes one query parameter id such as /mypage?id=hjhj.

What I am trying to achieve is to render that query parameter on /mypage?id=hjhj

router/index.js

const isServer = typeof window === 'undefined';
const history = isServer ? createMemoryHistory() : createWebHistory();
const routes = [{
        path: '/',
        name: 'Home',
        component: Home,
    },
    {
        path: '/about',
        name: 'About',
        component: About,
    },
    {
        path: '/mypage',
        name: 'mypage',
        component: MyPage,
        props: (route) => ({
            foo: route.query.foo
        }),
    }
];

const router = createRouter({
    history,
    routes,
});

export default router;

MyPage.vue

<template>
   <h2>Home</h2>
   <h3>{{ this.foo }}</h3>
</template>

<script>
   export default {
     props: {
       foo: {
         type: String,
         default: null,
       },
     },
   };
</script>

result:

enter image description here

I should see hjhj below "Home"

4
  • You could do something like router.vuejs.org/guide/essentials/… Commented Jul 24, 2022 at 12:39
  • Thanks but it seems that this documentation is for Path and there is nothing for Query parameters Commented Jul 24, 2022 at 12:48
  • Yes sorry. you can simply do this in the home template : $route.query.id Commented Jul 24, 2022 at 13:00
  • or in the setup method: import { useRoute } from 'vue-router'; export default { setup() { const route = useRoute(); console.log(route.query.id); } } Commented Jul 24, 2022 at 13:07

1 Answer 1

1

In templates we don't use this keyword to access the instance properties, as every variable available in template is proxied to the component instance.

<template>
   <h2>Home</h2>
   <h3>{{ foo }}</h3>
</template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.