Is there a way to display a single Vue component in two separate sub routes simultaneously?

const routes = [
{
    path: '/',
    component: LogInView
},
{
    path: '/store',
    component: Dashboard,
    children: [
        {
            path: '/products',
            component: ProductsView,
        },
    ]
},
{
    path: '/platform',
    component: Dashboard,
    children: [
        {
            path: '/products',
            component: ProductsView,
        },
    ]
},
{
    path: '/platform',
    component: Dashboard
} ]

Imagine I am setting to display the Dashboard component for both /store and /platform, and the ProductView component for /store/products and /platform/products. However, there seems to be a issue as when accessing the urls /store/products or /platform/products, it's actually rendering the Dashboard component.

Answer №1

Your code is having an issue where you are including a slash before each child route ('/products'). You should remove it to resolve the problem.

Answer №2

Your code still includes a component in the parent route with the children:

{
    path: '/store',
    component: Dashboard,
    children: [
        {
            path: '/products',
            component: ProductsView,
        },
    ]
},

However, according to the Vuejs documentation, the parent route should not have a component like this:

const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User,
      children: [
        {
          // UserProfile will be rendered inside User's <router-view>
          // when /user/:id/profile is matched
          path: 'profile',
          component: UserProfile
        },
        {
          // UserPosts will be rendered inside User's <router-view>
          // when /user/:id/posts is matched
          path: 'posts',
          component: UserPosts
        }
      ]
    }
  ]
})

Therefore, your code should look like this instead:

const routes = [
{
    path: '/',
    component: LogInView
},
{
    path: '/store',
    // component: Dashboard, // <-- should be removed
    children: [
        {
            path: 'products',
            component: ProductsView,
        },
    ]
},
{
    path: '/platform',
    children: [
        {
            path: 'products',
            component: ProductsView,
        },
    ]
},
{
    path: '/platform',
    component: Dashboard
} ]

@TEFO correctly mentioned that the child route should NOT start with a slash

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

Declare `document.body` as the designated container element for the material-ui Tooltip

The issue: I need to show a tooltip pointing to an element (referenceEl) from the left. The referenceEl is located within a container that has a limited width of 60px and has the css properties overflow: hidden applied. Due to the tooltip being appended af ...

Ensuring continuity of session in WebRTC audio calls post page refresh

Currently, I am utilizing the Kandy WebRTC library to facilitate audio calls through WebRTC. One issue I have encountered is maintaining the session alive if a user refreshes the page, as this JavaScript library operates internally using WebSocket. For in ...

Enlarging the current division while reducing the size of the others when hovering

Currently, I am working on a school project that involves creating a slideshow on a webpage. However, I have reached a point where I am unsure of how to proceed. Here is the progress I have made so far: body { background-color: #252525; } #wrapper { ...

Using Plotly.js to generate a visually stunning stacked and grouped bar chart

Is there a way to generate a bar chart on Plotly.js that features both grouped and stacked bars? Ideally, I am looking for a structure similar to the one shown here: Barchart with stacked and grouped charts ...

Encountering difficulties retrieving information from an API using Angular

I am encountering an issue while trying to retrieve data from an API. When I use console.log() to view the data, it shows up in the console without any problem. However, when I attempt to display this data in a table, I keep receiving the error message: ER ...

JavaScript - What is the best way to add the same object value into an array?

After spending a few hours trying to figure out how to manage JSON data structured like this: [ { "value": "Osteonecrosis", "Diagnosis_Code": "DIAG002", "NamaCategory": "Primary Category", "FK_Diagnosis_Content_ID": 2 }, { "value ...

What's the best way to incorporate a clear options icon when choosing an option in a MaterialUI select component?

I am looking to enhance the user experience by adding a clear options icon that appears when a selection is made from the list of options. <Select InputProps={{ startAdornment: ( <InputAdornment position='end'> <Cl ...

"Headers cannot be set once they have been sent to the client... Error handling for unhandled promise rejection

Having trouble with cookies in the header - specifically, encountering an error at line number 30. The error message reads: "Cannot set headers after they are sent to the client." Additionally, there is an UnhandledPromiseRejectionWarning related to a prom ...

Unit testing for changes in AngularJS $scope variables within the .then() function

I'm currently facing an issue with unit testing a function in my controller. The problem lies in making a $scope variable testable. I am assigning the variable within the .then() block of my controller and need to ensure it is set correctly when the . ...

Add hyphens to separate the words in AngularJS if there is a break in the string

Within a div of set width, a string is being bound to it. This string could be short or long. I would like for the string to break with a hyphen inserted on each line except for the last one. For example: If the string is "misconception" and it breaks at ...

Utilizing a potentially existing Angular service

Currently, I am engaged in a project using Angular that focuses on being highly modular. This means that different sections of the application can be enabled or disabled for various clients using Webpack. While this structure has been effective for me, I h ...

Searching and selecting columns in React using Material-UI

Currently, I am using Material-UI data tables and have implemented a search functionality similar to this Codesandbox example, which only searches the Name/Food column. This is my existing code snippet: const [filterFn, setFilterFn] = useState({ fn: items ...

Rearranging the order of Div elements within a main container using JavaScript DOM manipulation

How can I move elements within a div from the start to the end in the same div, for example, changing the order from 1-2-3 to 2-3-1? My code: const cards = document.querySelectorAll(".card"); const firstCard = document.querySelectorAll(".card")[0].inne ...

React - retrieving the previous page's path after clicking the browser's "back" button

Imagine I'm on Page X(/path-x) and then navigate to page Y(/path-y). Later, when I click the "back" button in the browser. So my question is, how do I retrieve the value of /path-y in PageX.tsx? Note: I am utilizing react-router-dom ...

Struggling to populate dropdown with values from array of objects

My issue is related to displaying mock data in a dropdown using the SUIR dropdown component. mock.onGet("/slotIds").reply(200, { data: { slotIds: [{ id: 1 }, { id: 2 }, { id: 3 }] } }); I'm fetching and updating state with the data: const ...

using database URL as an AJAX parameter

I am currently working on a Python CherryPy controller that needs to validate a database URL by attempting a connection. However, I am facing challenges with passing the parameter to the method. Below is my AJAX call: $.ajax({ async: false, ty ...

Can two Angular element components be utilized simultaneously on a single page in Angular 6?

Currently, I'm attempting to host independent Angular elements that can be seamlessly integrated into a non-Angular webpage. Everything works perfectly fine when there's only one element on the page, but as soon as I try to load two or more, I en ...

In order for elements in an iteration to function correctly, it is necessary to include the 'v-bind:key' directives as suggested by the eslint

Can anyone help me understand why I am receiving this error message? "[vue/require-v-for-key] Elements in iteration expect to have 'v-bind:key' directives.eslint-plugin-vue" I am encountering this issue on my post.vue file. [vue/requi ...

What's the best way to dynamically show Bootstrap collapse panels in a loop with AngularJS ng-repeat?

Currently, I am utilizing angularJS and attempting to include a bootstrap collapsible-panel within a loop. The code that I have written is causing all the panel bodies to be displayed beneath the first panel header. I need each body to be shown below i ...

select items using a dropdown menu in an Angular application

Let me describe a scenario where I am facing an issue. I have created an HTML table with certain elements and a drop-down list Click here for image illustration When the user selects in, only records with type in should be displayed Another image refere ...