Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing router.params on page refresh

I'm encountering an issue with vue-router.
I got a list of posts in the homepage. When I click on one of them, it redirects to the post page such as :

<router-link :to="{ name: 'article', params: {id: article.id, slug: article.slug } }"></router-link>

Everything is working perfectly, I can retrieve the data of the correct article using this.$route.params.id in a getter.

My issue : When I reload on an article page, this.$route.params.id is undefined, which causes the entire application to crash.

How can I save the router.params despite a page reload ?

like image 385
blickblick Avatar asked Apr 23 '19 14:04

blickblick


People also ask

How do I refresh my Vue component?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do you update data on a page without refreshing on the Vue JS?

What you need is vm-forceUpdate. Force the Vue instance to re-render. Note it does not affect all child components, only the instance itself and child components with inserted slot content.

How do you use a router to push?

Note: Inside of a Vue instance, you have access to the router instance as $router . You can therefore call this.$router.push . To navigate to a different URL, use router.push . This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.

What is VUEX in VUE JS?

Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.


2 Answers

What backend are you using? You need to enable history mode on your router, as well as make some additional configuration changes on your backend web server. Please refer to this link for the additional server side configuration changes you will need to make for this to work properly.

Also, please make note of this 404 caveat when using history mode..

Edit: you could try something like this since the ID remains persistent in the URL: Look for the solution from MIKE AXLE

I don't know if anyone else if facing the same issue, but I was having a problem getting route params on refresh. The route parameter I was trying to get was an ID number and I use that ID to fetch data and populate the page. I found (through many console logs) when I refreshed, the number was turning into a string and thats why the page was not working. I sorted it out but casting the ID to number before using it:

Number($route.params.id)

like image 123
Matt Oestreich Avatar answered Nov 15 '22 07:11

Matt Oestreich


You can also use query instead of params

<router-link :to="{ name: 'article', query: {id: article.id, slug: article.slug } }"></router-link>

and to get value on redirecting page

this.$route.query
like image 30
Ankush Kumar Avatar answered Nov 15 '22 08:11

Ankush Kumar