vuejs / router

🚦 The official router for Vue.js

Home Page:https://router.vuejs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect router params when using router.back()

watchingfun opened this issue · comments

Reproduction

https://codesandbox.io/p/devbox/nervous-bird-q4d89n

Steps to reproduce the bug

Jump to the route with two parameters, one of which is empty; then jump to the new route; then router.back().

  routes: [
    { path: "/", component: HelloWorld },
    { path: "/A/:p1?/:p2?",name:'A', component: A },
    { path: "/B", component: B },
  ]

When I jump to A, the parameters are {p1:'',p2:'p2'}. When I return using router.back(), the parameters should be the same as when I jumped. In fact, it is {p1:'p2',p2:''}

Expected behavior

When I jump to A, the parameters are {p1:'',p2:'p2'}. When I return using router.back(), the parameters should be the same as when I jumped.

Actual behavior

In fact, it is {p1:'p2',p2:''}

Additional information

No response

The problem comes from the path being non deterministic. Both version are possible when coming from the string version and that shouldn’t be the case. You need to add something to differentiate them: a prefix, a custom regexp:

  • /:p1?/p2/:p2?
  • /:p1(\d+)?/:p2? : first parameter can only be numbers