vue js可以将带有查询的路由转到组件吗?

时间:2018-09-14 17:16:43

标签: javascript vue.js vue-component vue-router

我想知道是否可以将带有查询的URL发送到组件页面吗?

例如,这是URL /physicians/?apptId=123&npi=123456789,我希望它转到BookAnAppointment.vue。我知道如果我定义了这样一个/physicians/:npi?apptId=123的路由,那是可行的,但这不是我想要的。

在PhysicianLanding页面上,如果我单击“ Book”按钮,它将把参数添加到URL,但是我不知道如何将其发送到BookAnAppointment组件。

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 个答案:

答案 0 :(得分:0)

如果您确实要使用相同的url,但只是在查询参数中有所不同,则可以根据路由及其顺序找到解决方案-添加相同的路由,但使用不同的名称,并在查询存在时通过钩子进行重定向,并且作为可选选项,您可以为 BookAnAppointment 组件动态设置道具:

    // 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

因此,当您在代码中使用路由器重定向按钮时,您可以直接使用路由“某些路由名称”:

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

或者,如果您使用基于url的重定向,它将通过第一个路由的钩子进行处理:

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