pinia实现todos

store/todos.js

//导入defineStore
import {defineStore} from 'pinia'
const userTodosStore=defineStore('todos',{
state:()=>({
    // list:[
    //     {id:1,name:'吃饭',done:false},
    //     {id:2,name:'睡觉',done:true},
    //     {id:3,name:'打豆豆',done:false}
    // ],
    list:JSON.parse(localStorage.getItem('todos')|| '[]'),
    filters:['全部','未完成','已完成'],
    active:'全部'
}),
actions:{
    //修改状态
    changeDone(id){
        const todo=this.list.find(item=>item.id===id)
        todo.done=!todo.done
    },
    //删除
    delTask(id){
        this.list=this.list.filter(item=>item.id !== id)
    },
    //添加
    addTask(taskname){
        this.list.push({
            id:Date.now(),
            name:taskname,
            done:false
        })
    },
    //全选
    changeAll(e){
        this.list.forEach(item=>item.done=e.target.checked)
    },
    //清除已完成
    clearDone(){
        this.list=this.list.filter(item=>!item.done)
    },
    //点击状态
    changeActive(active){
        this.active=active
    }

},
getters:{

    isAll(){
        return this.list.every(item=>item.done)
    },
    leftCount(){
        return this.list.filter(item=>!item.done).length
    },
    showList(){
        if(this.active==='已完成'){
            return this.list.filter(item=>item.done)
        }else if(this.active==='未完成'){
            return this.list.filter(item=>!item.done)
        }else{
            return this.list
        }
    }
}
    
})
export default userTodosStore

App.vue

<script setup>
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
</script>

<template>
  <section class="todoapp">
    <TodoHeader></TodoHeader>
    <TodoMain></TodoMain>
    <TodoFooter></TodoFooter>
  </section>
</template>

<style></style>

TodoMain.vue

<script setup>
import userTodosStore from '../store/todos'
// import {watch} from 'vue'
//获取store对象
const todos=userTodosStore()
//$subscribe 作用是监视store数据的变化,变化后会触发回调函数执行

// watch(todos.list,()=>{
//   localStorage.setItem('todos',JSON.stringify(todos.list))
// })
todos.$subscribe(()=>{
  localStorage.setItem('todos',JSON.stringify(todos.list))
})




</script>

<template>
  <section class="main">
    <input @change="todos.changeAll($event)" :checked="todos.isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li v-for="item in todos.showList" :key="item.id" :class="{completed:item.done}">
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done"
          @change="todos.changeDone(item.id)"
          />
          <label>{
  { item.name }}</label>
          <button class="destroy" @click="todos.delTask(item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
      <!-- <li>
        <div class="view">
          <input class="toggle" type="checkbox" />
          <label>Buy a unicorn</label>
          <button class="destroy"></button>
        </div>
        <input class="edit" value="Rule the web" />
      </li> -->
    </ul>
  </section>
</template>

<style lang="less" scoped></style>

ToDoHeader.vue

<script setup>
import {ref} from 'vue'
import userTodosStore from '../store/todos'

//获取store对象
const todos=userTodosStore()
const taskname=ref('')
const hKeydown=()=>{
if(taskname.value.length===0)return

  todos.addTask(taskname.value)
  taskname.value=''
}
</script>


<template>
  <header class="header">
    <h1>todos</h1>
    <input class="new-todo" 
    placeholder="What needs to be done?" 
    autofocus 
    v-model.trim="taskname"
    @keydown.enter="hKeydown"
    />
  </header>
</template>

<style lang="less" scoped></style>

ToDoFooter.vue

<script setup>
import userTodosStore from '@/store/todos';
const todos=userTodosStore()

</script>

<template>
  <footer class="footer">
    <span class="todo-count">还有<strong>{
  {todos.leftCount}}</strong> 个未完成</span>
    <ul class="filters">
      <li v-for="item in todos.filters" :key="item">
        <a @click="todos.changeActive(item)" :class="{selected:item === todos.active}" >{
  {item}}</a>
      </li>
      <!-- <li>
        <a href="#/active">Active</a>
      </li>
      <li>
        <a href="#/completed">Completed</a>
      </li> -->
    </ul>
    <button class="clear-completed " @click="todos.clearDone">清除已完成</button>
  </footer>
</template>

<style lang="less" scoped></style>

相关推荐

  1. react基础学习 附加todo实现代码

    2024-01-27 14:04:02       35 阅读
  2. IDEA <span style='color:red;'>TODO</span>

    IDEA TODO

    2024-01-27 14:04:02      33 阅读
  3. Vue3实战笔记(16)—pinia基本用法--Getter

    2024-01-27 14:04:02       11 阅读
  4. Vuex、Pinia

    2024-01-27 14:04:02       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 14:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 14:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 14:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 14:04:02       20 阅读

热门阅读

  1. XR虚拟拍摄技术:开启短剧与微剧的全新篇章

    2024-01-27 14:04:02       44 阅读
  2. 使用 Spark MLlib 使用 jieba 分词训练中文分类器

    2024-01-27 14:04:02       34 阅读
  3. SASS简介及使用方法

    2024-01-27 14:04:02       29 阅读
  4. 【办公自动化】Python中的BeautifulSoup

    2024-01-27 14:04:02       36 阅读
  5. PHP AES加解密:用代码为数据加上保护的盾牌

    2024-01-27 14:04:02       37 阅读
  6. 图像处理工具包Pillow的使用分享

    2024-01-27 14:04:02       36 阅读
  7. P2024 [NOI2001] 食物链 带权(种类)并查集整理

    2024-01-27 14:04:02       41 阅读
  8. MIT-Missing Semester_Topic 1: The Shell 练习题

    2024-01-27 14:04:02       29 阅读
  9. SpringBoot参数校验

    2024-01-27 14:04:02       34 阅读
  10. mysql数据库备份命令

    2024-01-27 14:04:02       41 阅读