lesson 213: store getters

This commit is contained in:
Andreas Zweili 2021-05-04 22:20:06 +02:00
parent b84be204f4
commit cdb9316050
3 changed files with 17 additions and 2 deletions

View File

@ -7,7 +7,7 @@
export default {
computed: {
counter() {
return this.$store.state.counter;
return this.$store.getters.normalizedCounter;
}
}
};

View File

@ -6,7 +6,7 @@
export default {
computed: {
counter() {
return this.$store.state.counter;
return this.$store.getters.finalCounter;
}
}
};

View File

@ -13,6 +13,21 @@ const store = createStore({
increase(state, payload) {
state.counter = state.counter + payload.value;
}
},
getters: {
finalCounter(state) {
return state.counter * 3;
},
normalizedCounter(_, getters) {
const finalCounter = getters.finalCounter;
if (finalCounter < 0) {
return 0;
}
if (finalCounter > 100) {
return 100;
}
return finalCounter;
}
}
});