Skip to content

Instantly share code, notes, and snippets.

@rwaddin
Last active October 27, 2022 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaddin/87b59f36629ab308ab9806574791c8e5 to your computer and use it in GitHub Desktop.
Save rwaddin/87b59f36629ab308ab9806574791c8e5 to your computer and use it in GitHub Desktop.
Vue3 Composition Api watch store value
<template>
<!-- trigger change value -->
<button @click="onChange">💎</button>
{{myvalue}} <!-- listen value terbaru -->
</button>
</template>
<script>
import {computed} from "vue";
import {useStore} from "vuex";
export default {
setup(){
const store = useStore();
store.watch((state, getters) => getters.fetchEpoch, (valBaru, valLama)=>{
console.log('value changes detected via vuex watch', [valBaru, valLama]);
})
return {
onChange: () =>{
// push perubahan
store.commit("PUT_EPOCH");
},
//get current value
myvalue: computed(() => store.getters.fetchEpoch),
}
}
}
<script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment