Skip to content

Instantly share code, notes, and snippets.

@Fanna1119
Last active April 7, 2021 13:15
Show Gist options
  • Save Fanna1119/05ba2a15f8a2395ef97bdb554f9153a2 to your computer and use it in GitHub Desktop.
Save Fanna1119/05ba2a15f8a2395ef97bdb554f9153a2 to your computer and use it in GitHub Desktop.
pinia todo
<template>
<input type="text" v-model="mytodo" />
<button @click="AddTodo">Add Todo</button>
<div v-if="!isEmpty">
<p v-for="(todo, index) in todos" :key="index">
{{ index }}. {{ todo }} <button @click="removeTodo(index)">delete</button>
</p>
</div>
<div v-else>No todos found</div>
</template>
<script>
import { computed, ref } from "vue";
import { useMainStore } from "./store.js";
export default {
setup() {
const main = useMainStore();
const mytodo = ref("");
const AddTodo = () => {
if (mytodo.value != "") {
main.addTodo(mytodo.value);
mytodo.value = "";
}
};
return {
AddTodo,
mytodo,
todos: computed(() => main.getAllTodos),
isEmpty: computed(() => main.todoEmpty),
removeTodo: main.removeTodo,
};
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment