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

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

我想知道是否可以将带有查询的 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>

如果你真的想使用相同的 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

因此,当您在代码中使用路由器来重定向用户点击按钮时,您可以直接使用路由 'some route name':

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