The path-to-regexp in vue-router fails to match an optional group of route path parameters

Exploring Vue.js Router

Currently, I am delving into the official vue-router of vue.js.

My focus is on implementing route matching using dynamic route matching. With vue-router utilizing path-to-regexp underneath its operations, regex can be employed in route paths, as explained here. An illustration of the use of this feature in vue-router can be found here.

The Challenge

This represents my current route configuration:

{
    path: '/a/:id/(r/:generic1/)?s/:name/a'
}

Here are some examples that produce successful matches:

'/a/1234/s/Foo/a' // => {0: undefined, id: "1234", name: "Foo"}
'/a/23456/s/Bar/a' // => {0: undefined, id: "23456", name: "Bar"}

However, the following examples should work but are not yielding the expected results:

'/a/1234/r/Bar/s/Foo/a' // => {id: "1234", generic1: "Bar", name: "Foo"}
'/a/23456/r/Baz/s/Goo/a' // => {id: "1234", generic1: "Baz", name: "Goo"}

What could be the issue here? One piece of advice mentioned states the following:

To make a part of the path optional, enclose it with parentheses and add "?" [sic]

In my understanding, these paths should align with the specified route.

Answer №1

As mentioned in this specific comment, it is not possible to include parameters within the regexp area.

One potential workaround could be using a structure like /a/:id/r?/:generic1?/s/:name/a. This pattern should cover all scenarios and retain the generic1 parameter as well.

To confirm the validity of this approach, I utilized the Express Route Tester tool.

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

Using JavaScript build-in functions in a Vue template allows for enhanced functionality and

Is there a way to utilize JavaScript built-in functions within a Vue template? {{ eval(item.value.substring(2)) }} I attempted to use the JS function eval() in {{}}, but encountered several errors such as: [Vue warn]: Property or method "eval" i ...

Animating the mobile menu transition using Bootstrap CSS

Can anyone help me with animating the mobile menu when I click the menu icon? This is what I have for the menu: .overlay-menu { position: fixed; display: none; z-index : 1040; width: 100vw; height: 100vh; background: rgba(0, 0,0, 0 ...

navigating between html pages using sliding next and previous buttons

I am in the process of developing a multi-page form spread across 4 different pages. I would like to incorporate next and previous buttons to allow users to easily navigate through the form. Although I have tried looking online for solutions, most results ...

Retrieving the value of a <select> element using React.useState in a Nextjs environment

Encountering an issue in Nextjs involving the useState function from React. There is a select element with multiple options. Upon selection, the useState should store the value of the selected option. const [value, setValue] = useState('') ... ...

Sails JS - Flash message display issue on Heroku in production environment, works smoothly in development mode

I'm experiencing an issue with the flash message on my local machine during development, as it works fine there but not when I deploy the app on Heroku. I've been searching for a solution without any luck so far. api/policies/flash.js module.ex ...

steps to invoke a class within a function

I'm currently attempting to invoke a model class from the Axios response. Instead of displaying an alert, I would like to show a modal popup. Can someone assist me with how to call the modal class from the Axios interceptor function? Thank you in adva ...

Get information that has been uploaded using ajax

I need help with my code for sending data via ajax to an update.php page. $(document).ready(function() { $("#modify").click(function() { var a = $("#a").val(); var b = $("#b").val(); var c = $("#c").val(); $.ajax({ type: "POST", ...

What methods can I use to emphasize three distinct dates?

I'm facing difficulty highlighting three specific dates: "10-11-2020", "22-11-2020", and "07-11-2020" using React.js and Datepicker. Unfortunately, my attempts have not been successful so far. Here is the code snippet I am working with: import React, ...

What are some methods for extracting a value from a separate webpage and saving it as a variable in my code?

I am utilizing graphite to obtain statistics and would like to display a justgage gauge for a specific variable. Graphite provides values in either JSON format: [{"target": "snmp.ssbSubstation.realEnergy.1", "datapoints": [[4511552439.0, 1417540920]]}] o ...

Sliding Toggle: Panel Revealed with Button Slide

Currently, I am working on implementing a jquery slide tab feature. The functionality is working fine on button click. However, I want the button to stick with the panel and slide along with it. At the moment, the button remains fixed while the panel slide ...

When the MATERIAL UI MENU ITEM is clicked, an action is triggered automatically upon loading the

I added an onClick event listener to a Menu Item in material UI, MUI. Upon loading the page, the onclick function triggers automatically. How can I resolve this issue? const [anchorEl, setAnchorEl] = useState(null) const open = Boolean(anchorEl) const ...

Transform a JQuery function using the each method into vanilla JavaScript

Seeking assistance. I have developed a multilingual static site using JQuery and JSON, but now I want to switch to simple JS. Most of the code is ready, except for the portion commented out in the JS (which works fine with JQuery). var language, transla ...

What is the reason behind getComputedStyle having visibility set to visible?

What is the reason behind getComputedStyle always returning element visibility as visible even when no explicit visibility has been set? For instance: getComputedStyle($('#block1')[0],null).visibility; --- "visible" Meanwhile: $('#block1&a ...

"Upon refreshing the browser, an Angular map loses its positioning and appears blank

I have implemented a LeafLet map in my Laravel 9 system, which is functioning properly with some exceptions. Here's how it looks like: https://i.stack.imgur.com/kcuVr.jpg The code in the controller (CompetitionsMapController.php) is as follows: ...

Color buffer can cause WebGL rendering to fail

<!DOCTYPE html> <html> <body> <canvas id="ctx" width="300" height="300"></canvas> <script> //Receiving Webgl Context var ctx = document.getElementById("ctx"); var webgl = ctx.getContext("exp ...

The MongoDB regex is failing to provide the expected outcome

I'm facing an issue with searching data in MongoDB. I have a table with approximately 5000 entries of data that need to be searched based on multiple columns with specific priority criteria. The first priorities for the search are symbol, name, co_nam ...

spinning camera around a globe with javascript and three.js

In my project, I am working on enabling a player to navigate in a first-person view on a planet using three.js and JavaScript. There are two key aspects I am focusing on: a) Implementing player movement around the planet, which involves two types of movem ...

Service Worker's fetch event is not triggered upon registering the service worker

Service Worker is a new concept to me. As I delved into learning how to incorporate Service Worker into My Next.js Application, I encountered an issue with the fetch event handler. Oddly enough, the fetch event handler doesn't trigger upon initially r ...

Exploring the power of computed properties and composables in Vue 3.2 through the setup script tag

Exploring the latest features of Vue (version 3.2) has been quite exciting for me. I recently developed a useFetch composable to leverage reusability based on the vue documentation. useFetch.js import { ref } from 'vue' import axios from ' ...

Exploring Angular 4's Capabilities: Navigating a Multi-Dimensional Array

I am currently working with a multi-dimensional array that has two keys, and it is structured as follows: user: any = {}; // The index is incremented within a for loop to add values to the user object (this part is functioning correctly) this.user[index++ ...