In my case I was using the name redirect, but couldn't receive the custom params because the target route had a redirect by path - in this case the provided params were lost.
So it was required to change from the 'path redirect' to the 'named redirect', but since 'path redirect' contained an additional data in the path, I now need to pass that data via the 'name redirect' as well using params.
So I ended up with a helper "redirectByName" function that preserves the params in this case:
redirectByName.js
//redirects by preserving params
export default (name, params = {}) => {
return (route) => ({
...route,
name,
params: {
...route.params,
...params
}
})
}
And here is an example how to use it:
myRoutes.js
import redirectByName from "./redirectByName";
...
export default return [
{
path:'some/path',
name: 'targetRouteName',
redirect:redirectByName('redirectRouteName', {tab: 'general'}), //optional additional parameters for redirection
},
{
path:'some/path/:tab',
name: 'redirectRouteName'
}]
The redirectByName basically takes the target route object to preserve all the passed data, overrides its name parameter with the provided one and merges the params. Then it passes the final route object back to accomplish the redirect.
metainstead ofmoreData?.push({meta: {foo: 1}})but after the transition$route.metais empty object ({})