Created
July 12, 2022 19:25
-
-
Save JeffreyWay/4e9ae3e183f85176ace959a7c31badb6 to your computer and use it in GitHub Desktop.
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
import { defineStore } from "pinia"; | |
export let useCounterStore = defineStore('counter', { | |
// data | |
state() { | |
return { | |
count: 5 | |
}; | |
}, | |
// methods | |
actions: { | |
increment() { | |
if (this.count < 10) { | |
this.count++; | |
} | |
} | |
}, | |
// computed | |
getters: { | |
remaining() { | |
return 10 - this.count; | |
} | |
} | |
}); |
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
<script setup> | |
import {useCounterStore} from "@/stores/CounterStore"; | |
let counter = useCounterStore(); | |
</script> | |
<template> | |
<div> | |
<h1>{{ counter.count }}</h1> | |
<button | |
@click="counter.increment()" | |
:disabled="! counter.remaining" | |
>Increment ({{ counter.remaining }} Remaining)</button> | |
</div> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice