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

这节来完成删除列表项,比较简单
视频地址:https://www.bilibili.com/video/BV1h7411N7bg?p=18


mutaions提供了两个removeItem实现方式。
一个用forEach+splice,一个用findIndex+splice
Vuex从入门到实战(十)——【实践篇】Todos05删除列表项_第1张图片

1、store/index.js——增加 removeItem 方法,根据 id 从 list 对象数组中删除对应的元素和它的属性。

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 = ''
    },
    // 我的实现
    removeItem1 (state, step) {
     
      state.list.forEach((e, index) => {
     
        if (e.id === step) {
     
          return state.list.splice(index, 1)
        }
      })
    },
    // 老师实现
    removeItem2 (state, step) {
     
      // 根据id查找对应的索引
      const i = state.list.findIndex(x => x.id === step)
      // 根据索引,删除对应的元素
      if (i !== -1) {
     
        state.list.splice(i, 1)
      }
    }
  },
  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" @click="removeItemById(item.id)">删除a>
      a-list-item>

      
      <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', 'removeItem1', 'removeItem2']),
    // 监听文本框内容变化
    handleInputChange (e) {
      
      console.log(e.target.value)
      this.setInputValue(e.target.value)
    },
    // 向列表项中新增 item 项
    addItemToList () {
      
      if (this.inputValue.trim().length <= 0) {
       // 判空处理
        return this.$message.warning('文本框内容不能为空')
      }
      this.addInputValue()
    },
    // 从列表中移除对应项
    removeItemById (id) {
      
      this.removeItem1(id)
    }
  }
}
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>

你可能感兴趣的:(#,Vuex基础入门教程笔记,vue,vuex)