Optional parameters in Vue Router

- Andrés Cruz

En español
Optional parameters in Vue Router

When you are working with Routes in Vue with Vue Router; surely you will be interested in defining parameters to your routes in Vue; and more than this, that some are optional; for that, all you have to do is place the ? in front of the parameter.

Optional parameters are a feature that allow you to define part of a route as optional when navigating between different components.

Optional parameters are defined using question marks (?) in the path. For example, if you have a /post/:id? feed, the id parameter is considered optional. This means that the /post path and the /post/123 path will both be valid.

When an optional parameter is used in a route, you can access its value in the corresponding component via this.$route.params . If the optional parameter is not present in the URL, its value will be undefined.

export const routes = [
...
    {
        name: 'home',
        path: '/test/:param1?',
        component: Home
    },
...
]

I just add a question mark ? will make it optional

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.