Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created July 12, 2022 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JeffreyWay/4e9ae3e183f85176ace959a7c31badb6 to your computer and use it in GitHub Desktop.
Save JeffreyWay/4e9ae3e183f85176ace959a7c31badb6 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>
@waseem759gb
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment