1

My use case is something like this.

  1. User come to questionPapersTable.vue and choose a paper name and hit on a Start exam button

enter image description here

  1. then the user is route to the startExam.vue component from there I want to pass that id parameter value to the axios as a parameter and want to display that id parameter in this StartExam.vue component. I tried

ID comes is : {{$route.query.id}}

But this isn't display anything and even not give an error in console. How do I achive this. This is my code.

questionPapersTable.vue

<template lang="html">
  <div class="">
    <h1>Hello</h1>
    <table>
      <tr>
        <th>Paper Name</th>
        <th></th>
      </tr>

      <tr v-for="n in 10">
        <td>Demo</td>
        <th><button @click="goStart(12)">Start exam</button></th>
      </tr>
    </table>
  </div>

</template>

<script>
export default {
  methods:{
    goStart(paperId){
      console.log("GO TO START EXAM!");
      this.$router.push({
        path:'/startExam',
        params:{
          id:paperId
        }

    });
    }
  }
}
</script>

startExam.vue

<template lang="html">
  <div class="">
  <h1>Start Exam</h1>
  ID comes is : {{$route.query.id}}
  </div>
</template>

<script>
import axios from 'axios'
export default {
  created(){
    axios.get('http://localhost/laravel_back/public/api/papers/' +id)
  }
}
</script>

<style lang="css">
</style>

1 Answer 1

4

params are ignored if a path is provided:

router.push({ name: 'startExamName', params: { docId }}) // -> /startExam/123
router.push({ path: `/startExam/${docId}` }) // -> /startExam/123

// This will NOT work
router.push({ path: '/startExam', params: { docId }}) // -> /startExam

Within in the javascript code if you want to access the id,

this.$route.params.id

https://router.vuejs.org/en/essentials/navigation.html

Sign up to request clarification or add additional context in comments.

5 Comments

i fixed it but I didn't get output to ID comes is : {{$route.query.id}}
How is your url structured now? If you are using params, it has to be {{$route.params.id}}
Now it's coming. =) I do a silly mistake I put {{$route.params.id}} instead of {{$route.query.id}}. But still I can't call that in created() hook. I tied to console log inside created lifecycle hook console.log("Inside created",$route.params.id); but it says $route is not defined
You should use this.$route.params.id.
Thanks a lot. It works! I highly appreciate your support. =)

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.