Skip to content

Instantly share code, notes, and snippets.

@polodev
Forked from JeffreyWay/CounterStore.js
Created August 1, 2023 05:04
Show Gist options
  • Save polodev/94935d99c2742422340ef86d52598736 to your computer and use it in GitHub Desktop.
Save polodev/94935d99c2742422340ef86d52598736 to your computer and use it in GitHub Desktop.
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;
}
}
});
<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