Skip to content

Instantly share code, notes, and snippets.

@selfagency
Created July 11, 2021 01:18
Show Gist options
  • Save selfagency/7a8352c5572193ca353e42e9f2a8daf5 to your computer and use it in GitHub Desktop.
Save selfagency/7a8352c5572193ca353e42e9f2a8daf5 to your computer and use it in GitHub Desktop.
[vuex subscribe]
<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