How to refresh view when vuex store changes?

by raven_corwin , in category: Javascript , a year ago

How to refresh view when vuex store changes?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , a year ago

@raven_corwin 

In order to refresh the view when the Vuex store changes, you can use a computed property in your component that depends on the data from the store.


Here's a step-by-step guide on how to achieve this:

  1. Import the necessary modules:
1
import { mapState } from 'vuex';


  1. Define the computed property in your component:
1
2
3
4
5
computed: {
  ...mapState({
    storeData: state => state.yourStoreModule.yourData
  })
}


Replace yourStoreModule with the name of your Vuex store module and yourData with the specific data property you want to watch.

  1. Use the computed property in your template:
1
2
3
4
5
<template>
  <div>
    {{ storeData }}
  </div>
</template>


  1. Whenever the data in the Vuex store changes, the computed property storeData will be automatically updated. This will trigger a re-rendering of the view, refreshing the displayed value.


Note: This assumes you have properly set up your Vuex store and mutations/actions to update the data.