Params field is empty in $router.push

Jozef Cipa picture Jozef Cipa · Nov 18, 2016 · Viewed 34.7k times · Source

Consider this:

this.$root.$router.push({
    path: '/dashboard', 
    params: { errors: 'error' }, 
    query: { test: 'test' }
})

I use this in my component to redirect to another URL, and some error has occured. The problem is that when I want to access params field in dashboard component, it's empty. The query field works well. I'm trying to access it by this.$route.params.errors.

Answer

euvl picture euvl · Nov 18, 2016

You can use params only with named paths (i think).

Example:

//route (in your router file should have "name")
{ path: '/errors', name: 'EXAMPLE', component: ... }

//navigating
this.$router.push({
    name: 'EXAMPLE', 
    params: { errors: '123' }
});

Now it will have correct value in this.$route.params.