vue js can a route with a query go to a component?

Viewed 1612

I want to know if it's possible to send a URL with a query to a component page?

For example, this is the URL /physicians/?apptId=123&npi=123456789 and I want it to go to BookAnAppointment.vue. I know it works if I have a route defined like this /physicians/:npi?apptId=123 but that's not what I want.

On the PhysicianLanding page if I click the "Book" button it will add the params to the URL but I can't figure out how to send it to the BookAnAppointment component.

router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import PhysicianLanding from '@/components/PhysicianLanding'
import PhysicianProfile from '@/components/PhysicianProfile'
import BookAnAppointment from '@/components/BookAnAppointment'

Vue.use(Router)

export default new Router({
  routes: [
    { 
      path: '/physicians',
      component: PhysicianLanding
    },
    { 
      path: '/physicians/profile/:url',
      component: PhysicianProfile
    },
    { 
      path: '/physicians/:npi',
      component: BookAnAppointment,
      props: true
    }
  ]
})

src/components/PhysicianLanding.vue

<template>
  <div class="container">
    <h1>{{ msg }}</h1>
    <!-- I know this works -->
    <button type="button" @click="$router.push({ path: '/physicians/' + physicianNpi, query: { appt_id: apptId }})">Book an Appointment</button>
    <!-- I want this one to work -->
    <button type="button" @click="$router.push({ path: '/physicians/', query: { appt_id: apptId, npi: physicianNpi }})">Book</button>
  </div>
</template>

<script>
  export default {
    name: 'PhysicianLanding',
    data () {
      return {
        msg: 'Welcome to the physicians landing page',
        apptId: '05291988',
        physicianNpi: '1346264132'
      }
    }
  }
</script>

src/components/BookAnAppointment.vue

<template>
  <div class="container">
    <h1>Book an Appointment</h1>
    <p>This is where you will book an appointment</p>

    <h2>Query Params</h2>
    <p>appt_id is {{ $route.query.appt_id }}</p>

    <button type="button" @click="$router.push({ path: '/physicians' })">Go back</button>
  </div>
</template>

<script>
  export default {
    name: 'BookAnAppointment',
    props: ['npi'],
    created () {
      console.log('npi is ' + this.$route.params.npi)
      console.log('appt_id is ' + this.$route.query.appt_id)
    },
    data () {
      return {}
    }
  }
</script>
1 Answers

If you really want to use same url but with difference in just query params there is solution based on routes and their order - add same route but with different name and hook to redirect if query exist, and as optional you can set props dynamically for BookAnAppointment component:

    // router
    { 
       path: '/physicians',
       component: PhysicianLanding,
       beforeEnter(to, from, next) {
          if (to.query && to.query.npi) {
              // redirect to route below
              next({ name: 'some route name', query: to.query })
          } else 
              next()
       }
    },
    { 
       path: '/physicians',
       component: BookAnAppointment,
       name: 'some route name',
       // add props
       props: (route) => ({ appt_id: route.query.appt_id, npi: route.query.npi })
    }
    // ... other routes

So when you use router in code to redirect user on button click you can directly use route 'some route name':

<button type="button" @click="$router.push({ name: 'some route name', query: { appt_id: apptId, npi: physicianNpi }})">Book</button>

Or if you use url based redirect it will be handled by hook from first route:

<button type="button" @click="$router.push({ path: '/physicians/', query: { appt_id: apptId, npi: physicianNpi }})">Book</button>
Related