lesson 213: store getters
This commit is contained in:
parent
b84be204f4
commit
cdb9316050
|
@ -7,7 +7,7 @@
|
|||
export default {
|
||||
computed: {
|
||||
counter() {
|
||||
return this.$store.state.counter;
|
||||
return this.$store.getters.normalizedCounter;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
export default {
|
||||
computed: {
|
||||
counter() {
|
||||
return this.$store.state.counter;
|
||||
return this.$store.getters.finalCounter;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Reference in New Issue