【问题标题】:vue js can a route with a query go to a component?vue js 可以将带有查询的路由转到组件吗?
【发布时间】:2019-02-19 12:57:22
【问题描述】:

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

例如,这是 URL /physicians/?apptId=123&npi=123456789,我希望它转到 BookAnAppointment.vue。我知道如果我有这样定义的路由 /physicians/:npi?apptId=123 会起作用,但这不是我想要的。

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

路由器/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>

【问题讨论】:

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


    【解决方案1】:

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

    因此,当您在代码中使用路由器在按钮单击时重定向用户时,您可以直接使用 route '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>
    

    【讨论】:

    • 我也在尝试这个解决方案,我似乎在props: (route) =&gt; { appt_id: route.query.appt_id, npi: route.query.npi }这一行遇到了编译器错误,它似乎期待一个 ;在appt_id: route.query.appt_id 之后而不是逗号
    • @mcallaro 是的,抱歉,现在已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2014-05-06
    • 2021-02-08
    • 1970-01-01
    • 2019-07-01
    • 2014-01-30
    • 2019-10-14
    相关资源
    最近更新 更多