vue-router
vue-router copied to clipboard
Param not present when redirecting multiple times
Version
2.7.0
Reproduction link
https://codesandbox.io/embed/xokkpop154?module=%2Frouter%2Findex.js
Steps to reproduce
- Create a route definition for
/
which redirects to/a
- Create a route definition for
/a
which redirects to/a/b/1
. - Create a route definition for
/a/b/:id
which redirects to/a/b/:id/c
. - Create a route definition for
/a/b/:id/c
which points to a component. - Navigate to
/
using your browser's address bar.
What is expected?
You end up at /a/b/1/c
.
What is actually happening?
You end up at /a/b/undefined/c
.
If using a function in step 3, params
is an empty object.
If you go directly to a/b/1
, it works correctly.
Thanks, for the moment, you can use a redirect
object to not lose the params: redirect: { name: 'f', params: {id: 1}}
the problem seems to be that the parameter being passed to the redirect is just the from
location but not the to location
I have encountered a limitation from this bug. I add this scenario to add an context. I have an vue js app with multiples routes and the locale in the URL.
Note: English (en) is my default locale.
"vue": "^2.6.10",
"vue-router": "^3.1.2"
Expectations
- "http://mydomain.com/" should redirect to "http://mydomain.com/en/home"
- "http://mydomain.com/en" should redirect to "http://mydomain.com/en/home"
- "http://mydomain.com/fr" should redirect to "http://mydomain.com/fr/home"
const DEFAULT_LOCALE = "en";
const routes: RouteConfig[] = [
{ path: "home", component: HomePage, name: "home" },
{ path: "", redirect: "home", name: "default" }
];
const localizedRoutes: RouteConfig[] = [
{
path: "/",
redirect: `/${DEFAULT_LOCALE}`
},
{
path: "/:locale([A-Za-z]{2})",
component: AppInternationalization, // template: `<router-view></router-view>`
children: routes
}
];
Current behavior
- "http://mydomain.com/" crash (blank page) with this warning message
[vue-router] missing param for redirect route with path "/:locale([A-Za-z]{2})/home": Expected "locale" to be defined
- "http://mydomain.com/en" works and redirect to "http://mydomain.com/en/home"
- "http://mydomain.com/fr" works and redirect to "http://mydomain.com/fr/home"
Workaround
if i apply the above workaround as explained below : Note: I m limited because i cannot (or dont know) how to retrieve the current this.$route.params.locale from this file because i m not in "Vue context" so instead I use the default locale
const routes: RouteConfig[] = [
{ path: "home", component: HomePage, name: "home" },
{ path: "", redirect: { name: "buy", params: { locale: DEFAULT_LOCALE } }, name: "default" } // Workaround: See https://github.com/vuejs/vue-router/issues/1762
];
Workaround behavior
- "http://mydomain.com/" works and redirects to "http://mydomain.com/en/home"
- "http://mydomain.com/en" works and redirect to "http://mydomain.com/en/home"
- "http://mydomain.com/fr" does nt works and redirect to "http://mydomain.com/en/home"
Any idea how to resolve this with the current behavior
I have the same problem,
and I will try to use workaround.
But the workaround is broken on now.
params will get {}
I replace redirect
to beforeEnter
at the end.
Reproduction link
https://codesandbox.io/s/vue-template-6fznn