Last active
October 27, 2022 04:14
-
-
Save rwaddin/87b59f36629ab308ab9806574791c8e5 to your computer and use it in GitHub Desktop.
Vue3 Composition Api watch store value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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