Vuex从入门到实战(九)——【实践篇】Todos04完成任务的新增

news/2024/7/10 1:22:25 标签: vue, vuex

本节完成任务的新增:点击添加事项完成 inputValue 到 list 的添加。
视频地址:https://www.bilibili.com/video/BV1h7411N7bg?p=17


注意点:
1、 list每个item都包含id、info、done。id是需要自增的,这节实现思路很巧妙。
2、完成新增之后记得清空输入框内容。
3、保存输入框内容到任务项时,使用trim剔除前后空格+判空处理。
在这里插入图片描述

1、store/index.js——增加变量nextId,增加 addInputValue 方法存储输入框内容到列表中。

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    inputValue: '', // 文本框内容
    nextId: 5 // list中id属性取值
  },
  mutations: {
    initList (state, step) {
      state.list = step
    },
    // 为 inputValue 赋值
    setInputValue (state, val) {
      state.inputValue = val
    },
    // 将inputValue存储到list中
    addInputValue (state) {
      const obj = {
        id: state.nextId,
        info: state.inputValue.trim(),
        done: false
      }
      state.list.push(obj)
      state.nextId++

      state.inputValue = ''
    }
  },
  actions: {
    getList (context) {
      axios.get('/list.json').then(({ data }) => {
        console.log(data)
        context.commit('initList', data)
      })
    }
  }
})

2、App.vue——添加按钮绑定addItemToList(),触发存储事件

<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="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 slot="footer" class="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>
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'app',
  data () {
    return {}
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue'])
  },
  methods: {
    ...mapMutations(['setInputValue', 'addInputValue', 'removeItem']),
    // 监听文本框内容变化
    handleInputChange (e) {
      // 拿到最新值,并同步到store
      console.log(e.target.value)
      this.setInputValue(e.target.value)
    },
    // 向列表项中新增 item 项
    addItemToList () {
      if (this.inputValue.trim().length <= 0) { // 判空处理
        return this.$message.warning('文本框内容不能为空')
      }
      this.addInputValue()
    }
  }
}
</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>


http://www.niftyadmin.cn/n/1486157.html

相关文章

我依旧喜欢折腾浏览器...

编码可以是一种工作&#xff0c;也可以是一种娱乐。在工作之余&#xff0c;每个人都有自己放松的方式&#xff0c;玩电动&#xff0c;打游戏&#xff0c;看书&#xff0c;听音乐...我也一样&#xff0c;喜欢看看电影&#xff0c;玩玩游戏&#xff0c;听听音乐放松自己。不过&am…

FMOD音频引擎简单使用

现代游戏已经不能没有声音&#xff0c;所以音频引擎成为游戏引擎中不可缺少的一部分&#xff0e;这是一篇介绍现代音频引擎的文章(http://hard.zol.com.cn/labs/2003/0520/60986.shtml)&#xff0e;FMOD音频引擎(http://www.fmod.org/)是一个非常不错的音频引擎&#xff0c;其使…

【js】forEach与for…in通过实例看用法,js删除对象数组的实现方式

js中常常需要删除对象数组中的某一个对象。 思路&#xff1a;根据id查找对应的索引&#xff1b;根据索引&#xff0c;删除对应的元素。 下面列出了几种查找索引的方式&#xff1a; 01 forEach onSubmit() { this.gradeIdList.forEach(item > {if(item.grade this.form.grad…

ssm框架的整合搭建(三)

mybatis逆向工程工具类的使用---mybatis generator 项目结构 配置文件 1 <?xml version"1.0" encoding"UTF-8"?>2 <!DOCTYPE generatorConfiguration3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"4 …

《让×××飞》经典影评

让飞&#xff0c;是什么意思&#xff1f;说全了&#xff0c;就是让再飞一会。如何理解&#xff1f;我已经开枪了&#xff0c;你什么都没有看到&#xff0c;不要着急&#xff0c;让再飞一会&#xff0c;你就会看到结果。给我时间。 事实上&#xff0c;这部电影就是&#xff0c;电…

Vuex从入门到实战(十)——【实践篇】Todos05删除列表项

这节来完成删除列表项&#xff0c;比较简单 视频地址&#xff1a;https://www.bilibili.com/video/BV1h7411N7bg?p18 mutaions提供了两个removeItem实现方式。 一个用forEachsplice&#xff0c;一个用findIndexsplice 1、store/index.js——增加 removeItem 方法&#xff0c…

每天一个小程序—0005题(批量处理图片大小)

第 0005 题&#xff1a; 你有一个目录&#xff0c;装了很多照片&#xff0c;把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 这个需要用到os模块&#xff0c;os模块的功能就是可以遍历目录和文件。 介绍一下我下面代码中用到的两个方法&#xff1a;① os.walk() 这个方法会返…

Vuex从入门到实战(十一)——【实践篇】点击checkbox改变公共数据list中done的值

这节完成点击checkbox改变公共数据list中done的值 注意点&#xff1a;checkbox 绑定 onChange() 如何拿到 e 的同时传递一个参数过去&#xff1f;【 一般onChange事件绑定时不必声明参数changeonChange’即可&#xff0c;下文onChange (e) {…} 】 1、store/index.js——增加…