Test Human
Test Human

Reputation: 31

Use same component for different route in vuejs

I want to use the same component for the different routes i.e - I have 2 routes

  1. http://example.com/users/39
  2. http://example.com/users/39/58

Now I want to render the same component if a user visits one of the above route. I tried the below code and it's working fine for me, but it's not the proper solution. Is there any way to achieve in a single route?

<script>
{
    path: ':id',
    component: () => import('@/views/Users')
},
{
    path: ':id/:user_id',
    component: () => import('@/views/Users')
}
<script>

Upvotes: 0

Views: 668

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50767

You can declare an optional argument:

{
    path: '/users/:id/:user_id?',
    component: () => import('@/views/Users')
}

This will work for /users/39 and /users/39/58

Upvotes: 3

Related Questions