When working with vue-router, I am faced with the challenge of creating a route that can handle multiple optional parameters. For example, the route needs to be able to handle scenarios like:
/something/a/1/b/2/c/3 /something/a/1/b/2 /something/a/1/c/3 /something/b/2/c/3 /something/a/1 /something/b/2 /something/c/3 /something/
In these examples, letters represent keys and digits represent values.
This is how I approached the problem in my code:
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/something/a/:a/b/:b/c/:c', component: MyComponent },
{ path: '/something/a/:a/b/:b', component: MyComponent },
{ path: '/something/a/:a/c/:c', component: MyComponent },
{ path: '/something/b/:b/c/:c', component: MyComponent },
{ path: '/something/a/:a', component: MyComponent },
{ path: '/something/b/:b', component: MyComponent },
{ path: '/something/c/:c', component: MyComponent },
{ path: '/something/', component: MyComponent },
]
})
I wonder if there is a way to achieve this using regex. Perhaps I could just use '/something/*'
and then extract the parameters later? Also, all values are integers in this case.