Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you force Vue.js to reload/re-render?

Just a quick question.

Can you force Vue.js to reload/recalculate everything? If so, how?

like image 826
Dave Avatar asked Aug 19 '15 21:08

Dave


People also ask

Can you force Vue JS to reload Rerender?

Unfortunately, there are no simple ways to force-reload components properly in Vue.

How do I force re-render in Vue?

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 I remount a component Vue?

To remount a component in Vue. js, we should change the key prop of the component. to set the key prop of my-component to componentKey . And then we add a button to toggle the componentKey between true and false .

Does Vue re-render on Prop change?

Using the key changing technique If the key stays the same, it won't change the component, but if the key changes, Vue knows that it should get rid of the old component and re-create a new one. Every time that forceRerender is called, our prop componentKey will change.


2 Answers

Try this magic spell:

vm.$forceUpdate(); //or in file components this.$forceUpdate(); 

No need to create any hanging vars :)

Update: I found this solution when I only started working with VueJS. However further exploration proved this approach as a crutch. As far as I recall, in a while I got rid of it simply putting all the properties that failed to refresh automatically (mostly nested ones) into computed properties.

More info here: https://vuejs.org/v2/guide/computed.html

like image 192
Boris Mossounov Avatar answered Oct 10 '22 04:10

Boris Mossounov


This seems like a pretty clean solution from matthiasg on this issue:

you can also use :key="someVariableUnderYourControl" and change the key when you want to component to be completely rebuilt

For my use case, I was feeding a Vuex getter into a component as a prop. Somehow Vuex would fetch the data but the reactivity wouldn't reliably kick in to rerender the component. In my case, setting the component key to some attribute on the prop guaranteed a refresh when the getters (and the attribute) finally resolved.

like image 43
Brian Kung Avatar answered Oct 10 '22 04:10

Brian Kung