Exploring Nested Routes and Queries in Vue JS

As a beginner in Vue, I have been exploring a demo project and struggling with understanding how routes with query parameters function. The documentation suggests using

router.push({ path: 'register', query: { plan: 'private' }})
to generate the URL /register?plan=private.

I'm curious if it's possible to achieve similar functionality with nested routes.

My goal is to set the URL for the BookAppointment component as:

/physicians/?appt_id=12345&npi=123456789
. Any suggestions on a better approach would be greatly appreciated. Thank you!

router/index.js


const router = new VueRouter({
  routes: [
   { path: '/physicians/', component: PhysicianLanding,
     children: [
      {
        // PhysicianProfile
        path: 'profile/:url',
        component: PhysicianProfile
      },
      {
        // BookAppointment has 3 different progress steps to get to
        // the confirm page
        path: '',
        query: { appt_id: '12345', npi: '123456789'},
        component: BookAppointment
      }
    ]
   }
 ]
})

Answer №1

const router = new VueRouter({
  routes: [
   { path: '/doctors/', component: DoctorLanding,
     children: [
      {
        // DoctorProfile
        path: 'profile/:url',
        component: DoctorProfile
      },
      {
        // ScheduleAppointment has multiple progress steps to reach
        // the confirmation page
        path: '',
        //query: { appt_id: 'ABCDE', license: '123456'}, // not needed here.
        component: ScheduleAppointment
      }
    ]
   }
 ]
})

To access the ScheduleAppointment Component with the URL as ->

/doctors/?appt_id=ABCDE&license=123456
, you can create a button/link in the Vue template with the following @click event:

<button 
  @click="$router.push({ path: '/doctors/', query: { 
   appt_id: ABCDE, 
   license: 123456
  }})"
>
 GO TO: Schedule Appointment
</button>

OR

<button 
 @click="$router.push({ path: '/doctors/?appt_id: ABCDE&license: 123456})"
>
 GO TO: Schedule Appointment
</button>

You have the flexibility to change the query values, while the ScheduleAppointment component will still be displayed.

For instance,

/doctors/?appt_id: FGHIJ&license: 0987654321
will also show the ScheduleAppointment Component.

You can utilize different query values to retrieve diverse data from the database and exhibit it on the same fundamental template.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

Tips for using an array as a jQuery selector in JavaScript

Managing an element with an array id id="x[]" can be tricky when it comes to dynamically changing content based on database entries. This specific element is essentially a delete button for removing table rows from the database. <div align="center" id= ...

Replace Original Image with Alternate Image if Image Error Occurs Using jQuery (Bootstrap File Input)

I am currently utilizing the Bootstrap File Input component for file uploads, and it's working splendidly. I have set up the initialPreviewConfig to display the existing image on the server. However, there are instances when there is no file available ...

How can I save or export an image file containing HTML content?

Currently, I am working on a project where I am generating dynamic HTML content. My goal is to be able to export or save this HTML content as an image file using PHP, jQuery, and JavaScript (or any other method if applicable). Can anyone help with the im ...

I created a Vue object, but I seem to have misplaced it

I have a question about my code. I am trying to run Webpack and turn my main.js file into stem.js. Unfortunately, it's not working as expected. My app seems to be missing. Can anyone help me troubleshoot this issue? ...

eliminate currency match regular expression

Currently, I've implemented this regular expression to validate dollar amount inputs. parsley-regexp="^\$?[0-9][0-9\,]*(\.\d{1,2})?$|^\$?[\.]([\d][\d]?)$" I am now looking to modify the validation so that onl ...

Using v-model within a nested v-for loop of a multi-level array

Hey there! I'm looking to create a dynamic table for managing employee meals based on the selected month. I've made some progress, but I'm encountering an issue where marking meals for one employee affects all other employees. It seems like ...

Move tables by dragging and dropping them into place

Currently, I am on the lookout for a drag and drop plugin that works with jQuery, Angular, or JavaScript to help me create dynamic tables. Specifically, I need a plugin that allows me to add new tables through drag and drop functionality. While I have com ...

Transmitting document through HTML form using Laravel integration with Mailgun API

I'm currently working on a feature where users can select a file via an HTML form and have it sent as an email attachment using Mailgun. I want to avoid storing the file on my server. The goal is to allow for uploading various file types such as PNG, ...

The chosen state does not save the newly selected option

Operating System: Windows 10 Pro Browser: Opera I am currently experiencing an issue where, upon making a selection using onChange(), the selected option reverts back to its previous state immediately. Below is the code I am using: cont options = [ ...

Using JS and d3.js to eliminate or combine duplicate connections in a d3.js node diagram

Hey there! Hello, I am new to working with js/d3.js and tackling a small project. Here are some of my previous inquiries: D3.js: Dynamically create source and target based on identical JSON values JS / d3.js: Steps for highlighting adjacent links My cu ...

In a production environment, an ENOENT error in Next.js was triggered by fs.readdirSync

Utilizing the Next.js App Router, I attempted to retrieve all my markdown posts stored in files by scanning a directory using fs.readdirSync(). While everything worked flawlessly locally, upon deploying on Vercel, an unexpected issue arose. The code was e ...

Require assistance in getting a slider operational

Hello there! I could really use your assistance with this code. I have been trying to make it work using JavaScript, but I'm determined not to incorporate any external JS files. Here is the snippet of JavaScript code I am working with: "use strict"; ...

Modify the `div` content based on the selected items from the bootstrap dropdown menu

My navigation bar is built using Bootstrap and contains multiple drop-down items. Now I have another div with the class of col-md-3. Within this div, I would like to display the items of the dropdown menu when hovered over. Currently, hovering over a dro ...

deleting the current product information during an ajax request

After executing the query provided below, I have $product_list in this code. However, I want to use ajax so that when I click on the button link1, $product_list gets emptied. How can I clear the content within products_list using an ajax call triggered by ...

Modify the CSS class by adding attributes dynamically instead of directly to the element

I encountered a situation where I needed to apply a css property to a class rather than an element. For example: $('.class_with_css').css({display:'none'}); This code would add the style "display:none" to all elements with the cl ...

Attempting to update the state by utilizing the values provided by useContext

Hey there! I'm currently working on a Gatsby React app and my main objective on this particular page is to remove some localStorage and reset context AFTER rendering. The timing of this reset is crucial because I need the page to initially render with ...

Trouble Arising from the Lack of Coordination Between CSS Transition and JavaScript Update Triggered by Element

I'm currently working on a web development project that involves a list of clickable elements. When one of these elements is clicked, it should become active and trigger a CSS transition (such as a transform) with a duration of 200ms. Additionally, I ...

Automating testing with JavaScript and Selenium WebDriver

Can testing be automated using the combination of JavaScript and Selenium? I am not familiar with Java, Python, or C#, but I do have expertise in Front-End development. Has anyone attempted this before? Is it challenging to implement? Are there any recom ...

I need to update my database by sending requests to my API, as the changes are not being reflected in the current database state. I am eager to see the

Struggling to grasp the concept of making requests to an API that uses express for CRUD operations. In the code snippet below, .get(/bears) displays a form and in app.post('/bears'), I'm using the 'request' module to send a request ...