溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vuex如何實現(xiàn)記事本功能

發(fā)布時間:2022-05-23 09:26:06 來源:億速云 閱讀:164 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Vuex如何實現(xiàn)記事本功能的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Vuex如何實現(xiàn)記事本功能文章都會有所收獲,下面我們一起來看看吧。

首先:執(zhí)行命令 安裝Vuex

npm install vuex@next --save

在mian.js 中掛在vuex

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

這里使用的 Ant Design UI :

npm install ant-design-vue --save

在 main.js 中完整引入

import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
Vue.use(Antd)

App.vue中

<template>
  <div id="app">
    <div>
      <a-input placeholder="請輸入任務(wù)" class="my_ipt" :value='inputVal'
      @change="handleInputChange"
      />
      <a-button type="primary" @click="addItem">添加事項</a-button>
 
      <a-list bordered :dataSource="infoList" class="dt_list">
        <a-list-item slot="renderItem" slot-scope="item">
          <!-- 復(fù)選框 -->
          <a-checkbox :checked='item.done' @change="changeItem(item.id,$event.target.checked)">{{ item.info }}</a-checkbox>
          <!-- 刪除鏈接 -->
          <a slot="actions" @click="deleteItemById(item.id)">刪除</a>
        </a-list-item>
 
        <!-- footer區(qū)域 -->
        <div class="footer" slot="footer">
          <span>{{unDoneNub}}條未完成</span>
          <a-button-group>
            <a-button :type="ViewType=='all'?'primary':''" @click="changeList('all')">全部</a-button>
            <a-button :type="ViewType=='undone'?'primary':''" @click="changeList('undone')">未完成</a-button>
            <a-button :type="ViewType=='done'?'primary':''" @click="changeList('done')">已完成</a-button>
          </a-button-group>
          <a @click="deleteDone">清除已完成</a>
        </div>
      </a-list>
    </div>
  </div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
      // 模擬數(shù)據(jù)
      // list: []
    }
  },
  computed: {
    ...mapState(['list', 'inputVal', 'ViewType']),
    ...mapGetters(['unDoneNub', 'infoList'])
  },
  created () {
    this.$store.dispatch('getList')
  },
  methods: {
    handleInputChange (e) {
      console.log(e.target.value)
      // 拿到輸入框的值  保存到vuex中
      this.$store.commit('setInputVal', e.target.value)
    },
    // 向列表中添加事項
    addItem () {
      if (this.inputVal.trim().length <= 0) {
        return alert('文本框不能為空')
      }
      // 向store中調(diào)用函數(shù)  來修改數(shù)據(jù)  不可以直接修改
      this.$store.commit('addItem')
    },
 
    // 刪除
    deleteItemById (id) {
      // console.log(id);
      this.$store.commit('deleteItem', id)
    },
 
    // 改變狀態(tài)
    changeItem (id, e) {
      console.log(id, e)
      // 通過id改變狀態(tài)
      this.$store.commit('changeItem', id)
    },
 
    // 清除已完成
    deleteDone () {
      this.$store.commit('deleteDone')
    },
 
    changeList (type) {
      this.$store.commit('changeList', type)
    }
 
  }
}
</script>
<style scoped>
#app {
  padding: 10px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
}
.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>

store index.js 中

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
 
Vue.use(Vuex)
 
export default new Vuex.Store({
  state: {
    list: [],
    inputVal: '',
    id: 10,
    ViewType: 'all'
  },
  // 真正操作數(shù)據(jù)的地方
  mutations: {
    INITLIST (state, data) {
      state.list = data
    },
    setInputVal (state, data) {
      state.inputVal = data
    },
    addItem (state) {
      const obj = {
        id: state.id,
        info: state.inputVal.trim(),
        done: false
      }
      state.list.push(obj)
      state.id++
      state.inputVal = ''
    },
    // 刪除已完成
    deleteDone (state) {
      state.list = state.list.filter(item => {
        return item.done != true
      })
    },
    deleteItem (state, id) {
      state.list = state.list.filter(item => {
        // console.log(item.id);
        return item.id != id
      })
    },
    // 改狀態(tài)
    changeItem (state, id) {
      // 對應(yīng)id的done值取反 先拿索引 根據(jù)索引 取反對應(yīng)的狀態(tài)  如果有多重狀態(tài) 則需要參數(shù)傳遞
      const index = state.list.findIndex(item => {
        return item.id === id
      })
      if (index !== -1) {
        state.list[index].done = !state.list[index].done
      }
    },
    // 改變列表
    changeList (state, type) {
      state.ViewType = type
      state
    }
  },
  actions: {
    //模仿發(fā)送請求
    getList (content) {
      axios.get('./list.json').then(res => {
        console.log(res.data)
        content.commit('INITLIST', res.data)
      })
    }
 
  },
  modules: {
  },
  getters: {
    // 未完成的數(shù)量
    unDoneNub (state) {
      return (state.list.filter(item => {
        return item.done == false
      })).length
    },
    // 根據(jù)列表類型 過濾不同的展示列表
    infoList (state) {
      if (state.ViewType == 'all') {
        return state.list
      }
      if (state.ViewType == 'undone') {
        return state.list.filter(item => !item.done)
      }
      if (state.ViewType == 'done') {
        return state.list.filter(item => item.done)
      }
    }
  }
})

list.json

[
    {
        "id": 0,
        "info": "打籃球",
        "done": false
    },
    {
        "id": 1,
        "info": "打王者榮耀",
        "done": true
    },
    {
        "id": 2,
        "info": "學(xué)習(xí)",
        "done": false
    },
    {
        "id": 3,
        "info": "吃飯",
        "done": false
    },
    {
        "id": 4,
        "info": "睡覺",
        "done": false
    }
]

結(jié)果圖:

Vuex如何實現(xiàn)記事本功能

關(guān)于“Vuex如何實現(xiàn)記事本功能”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Vuex如何實現(xiàn)記事本功能”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI