Vuex案例————todos(任务事项)

业务实现:

  1. 可添加任务
  2. 根据id删除对应的任务事项
  3.  清除列表中已完成的任务
  4. 统计未完成的任务的条数
  5. 修改页面上展示的数据列表(全部、未完成,已完成)

页面最终效果 

创建vuex项目,安装相关依赖:(axios、ant-design-vue(UI库))

  • npm install axios
  • npm install ant-design-vue@1.x   、
     

基本代码素材:

main.js

import Vue from 'vue'
import App from './App.vue'
import store from './store/index'

//1、导入ant-design-vue 组件库
import Antd from 'ant-design-vue'

//2、导入组件库的样式表
import 'ant-design-vue/dist/antd.css'

Vue.config.productionTip = false
//3、安装组件库
Vue.use(Antd)

new Vue({
  render: h => h(App),
  store 
}).$mount('#app')

 App.vue

<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" />
    <a-button type="primary">添加事项</a-button>

    <a-list bordered :dataSource="list" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox>{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div class="footer" slot="footer">
        <span>0条剩余</span>
        <a-button-group>
          <a-button type="primary">全部</a-button>
          <a-button>未完成</a-button>
          <a-button>已完成</a-button>
        </a-button-group>
        <a>清除已完成</a>
      </div>
    </a-list>
  </div>
</template>
<script>
export default {
  name: "app",
  data() {
    return {
      list: [
        {
          id: 0,
          info: "Racing car sprays burning fuel into crowd.",
          done: false
        },
        {
          id: 1,
          info: " Japanese princess to wed commoner.",
          done: false
        },
        {
          id: 2,
          info: "Australian walks 100km after outback crash.",
          done: false
        },
        {
          id: 3,
          info: "Man charged over missing wedding girl.",
          done: false
        },
        {
          id: 4,
          info: "Los Angeles battles huge wildfires.",
          done: false
        },
      ],
    };
  },
};
</script>
<style scoped>
#app {
  padding: 10px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}
.dt_list {
  width: 500px;
  margin-top: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

完整代码:

main.js

import Vue from 'vue'
import App from './App.vue'
import store from './store/index'
//1、导入ant-design-vue 组件库
import Antd from 'ant-design-vue'

//2、导入组件库的样式表
import 'ant-design-vue/dist/antd.css'

Vue.config.productionTip = false
//3、安装组件库
Vue.use(Antd)

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

store.js

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //所有任务列表
    list:[],
    //文本框的内容
    inputValue:'',
    //下一个id
    nextId:5,
    //选择框
    viewKey:'all'
  },
  mutations: {
    initList(state,list){
      state.list=list
    },
    //为store中的inputValue赋值
    setinputValue(state,val){
      state.inputValue=val
    },
    //添加列表项
    addItem(state){
      const obj={
        id:state.nextId,
        info:state.inputValue.trim(),
        done:false
      }
      state.list.push(obj)
      state.nextId++
      state.inputValue=''
    },
    //根据id删除对应的任务事项
    removeItem(state,id){
      //根据id查找对应项的索引
      const i=state.list.findIndex(x=>x.id===id)
      //根据索引,删除对应元素
      if(i!==-1){
        state.list.splice(i,1)
      }
    },
    //修改列表项选中状态
    schangeStatus(state,param){
      const i=state.list.findIndex(x=>x.id===param.id)
      if(i !==-1){
        state.list[i].done=param.status
      }
    },
    //清除列表中已完成的任务
    cleanDone(state){
      state.list=state.list.filter(x=>x.done===false)
    },
    //修改视图的关键字
    changeViewKey(state,key){
      state.viewKey=key
    }
  },
  actions: {
    getList(context) {
      axios.get('/list.json').then((res) => {
        console.log(res.data);
        context.commit('initList',res.data)
      })
    }
  },
  getters: {
    //统计未完成的任务的条数
    unDoneLength(state){
      return state.list.filter(x=>x.done===false).length
    },
    //
    infoList(state){
      if(state.viewKey==='all'){
        return state.list
      }
      if(state.viewKey==='undone'){
        return state.list.filter(x=>x.done===false)
      }
      if(state.viewKey==='done'){
        return state.list.filter(x=>x.done===true)
      }
      return state.list
    }
  },

})

 App.vue

<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handleInputChange" />
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e) => { cbstatusChanged(e, item.id) }">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div class="footer" slot="footer">
        <span>{{ unDoneLength }}条剩余</span>
        <a-button-group>
          <a-button :type="viewKey === 'all' ? 'primary' : 'default'" @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey === 'undone' ? 'primary' : 'default'" @click="changeList('undone')">未完成</a-button>
          <a-button :type="viewKey === 'done' ? 'primary' : 'default'" @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>
<script>
import { mapState, mapGetters } from 'vuex';
export default {
  name: "app",
  data() {
    return {
    };
  },
  methods: {
    handleInputChange(e) {
      // console.log(e.target.value);
      this.$store.commit('setinputValue', e.target.value)
    },
    //向列表中新增item项
    addItemToList() {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空')
      }
      this.$store.commit('addItem')
    },
    //根据id删除对应的任务事项
    removeItemById(id) {
      // console.log(id);
      this.$store.commit('removeItem', id)
    },
    //监听复选框选中状态变化事件
    cbstatusChanged(e, id) {
      // 通过e.target.checked 可以接收最新的选中状态
      // console.log(e.target.checked);
      // console.log(id);
      const param = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit('schangeStatus', param)
    },
    //清除已完成的任务
    clean() {
      this.$store.commit('cleanDone')
    },
    //修改页面上展示的数据列表
    changeList(key) {
      // console.log(key);
      this.$store.commit('changeViewKey', key)
    }

  },
  created() {
    this.$store.dispatch('getList')
  },
  computed: {
    // ...mapState(['list', 'inputValue', 'viewKey']),
    ...mapState([ 'inputValue', 'viewKey']),
    ...mapGetters(['unDoneLength','infoList'])
  }
};
</script>
<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3 Todos案例是一个使用Vue3编写的TodoList应用程序。该应用程序允许用户添加、编辑和删除待办事项,并可标记已完成的待办事项。 在该案例中,我们首先使用Vue3的Composition API来定义相关的响应式数据。我们需要一个代表待办事项列表的数组todos,以及一个用于输入新待办事项内容的变量newTodo。 然后,我们定义了几个处理用户操作的函数。addTodo函数用于将新待办事项添加到todos数组中,将newTodo重置为空字符串。editTodo函数用于编辑待办事项的内容,它接收一个索引作为参数,并将todos数组中对应索引的待办事项用newTodo的值替换。deleteTodo函数用于删除待办事项,它接收一个索引作为参数,并使用splice方法从todos数组中删除对应索引的待办事项。 在模板中,我们使用v-for指令遍历todos数组,使用v-model指令将newTodo与输入框进行双向绑定,使用@click指令监听按钮点击事件,并调用相应的函数。 最后,我们通过创建一个Vue实例来挂载这个TodoList组件,并将其渲染到HTML页面中。 Vue3 Todos案例通过使用Vue3的新特性Composition API,使得组件的逻辑更加清晰和可维护。它展示了Vue3在构建响应式应用程序方面的优势,并且在性能方面也有所提升。同时,它还能够帮助开发者更好地理解和运用Vue3的Composition API,为开发更复杂的应用打下基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

娃哈哈哈哈呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值