【发布时间】: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