Created
July 11, 2021 01:18
-
-
Save selfagency/7a8352c5572193ca353e42e9f2a8daf5 to your computer and use it in GitHub Desktop.
[vuex subscribe]
This file contains 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> | |
<h1 v-if="status === 'success'">Success {{ complex.deep }}</h1> | |
<h1 v-else-if="status === 'error'">Error</h1> | |
<h1 v-else>Loading</h1> | |
</template> | |
<script> | |
import { mapState } from 'vuex'; | |
export default { | |
name: 'vuex4', | |
data() { | |
return { | |
complex: null, | |
}; | |
}, | |
computed: mapState(['status']), | |
mounted() { | |
this.$store.subscribe((mutation, state) => { | |
switch(mutation.type) { | |
case 'updateStatus': | |
const status = state.status; | |
console.log(`Updating to ${status}`); | |
// Do whatever makes sense now | |
if (status === 'success') { | |
this.complex = { | |
deep: 'some deep object', | |
}; | |
} | |
break; | |
} | |
}) | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment