Vuex说明及Todos项目改造
Vuex(vue) / Flux (angular) /Redux(react)
vuex 是什么?
-
狀態管理工具
-
狀態即數據, 狀態管理就是管理組件中的data數據
-
Vuex 中的狀態管理工具,采用了 集中式 方式統一管理項目中組件之間需要通訊的數據
-
[看圖]
如何使用
- 最佳實踐 : 只將組件之間共享的數據放在 vuex 中, 而不是將所有的數據都放在 vuex 中 ,
- 也就是說:如果數據只是在組件內部使用的,這個數據應該放在組件中,而不要放在 vuex
- vuex 中的數據也是響應式的,也就是說:如果一個組件中修改了 vuex 中的數據,另外一個使用的 vuex 數據的組件,就會自動更新 ( vuex 和 localstorage的區別)
什么時候用 ?
-
官網
-
說明: 項目體量很小,不需要使用 vuex, 如果項目中組件通訊不復雜,也不需要使用 vuex
-
只有寫項目的時候,發現組件通訊多,組件之間的關系復雜,項目已經無法繼續開發了,此時,就應該使用 vuex
Vuex的基本使用
1. vuex的基本使用
- 引入文件
- 使用 vuex 插件
- 創建 store
- 關聯 vm 和 store
2. state
vuex通過state來提供數據 類似于組件的data
- 創建store的時候,可以指定state
- 可以在任意組件的模板中,訪問到vuex中state的數據
- 事件中
3. mutation
####3.1 演示報錯
- 演示1 - 添加嚴格模式
- 演示2 : 修改
3.2 mutation使用
- 創建store的時候,需要提供mutations
- mutation中所有的方法的第一個參數,都是state, 可以修改state里面的數據
- 組件中不能直接修改state,但是可以提交mutation,類似于子組件觸發事件
4. vuex 傳參
- 傳參
- 接收
Todos 改造
Todos碼云地址:https://gitee.com/wang_yu5201314/tudos_potato_silk_case
1. 初始化項目
- 創建項目
- 組件化開發
- 把結構和樣式都拷貝過來并且引入
- 組件分為三個組件 : TodoHeader TodoList TodosFooter
2. 配置 vuex
- 安裝 vuex :
- 創建 store/index.js
Todos 步驟
##1. 列表展示
<li :class="{completed : item.done}" v-for="item in $store.state.list" :key="item.id"><div class="view"><input class="toggle" type="checkbox" checked v-model="item.done" /><label>{{ item.name }}</label><button class="destroy"></button></div><input class="edit" value="Create a TodoMVC template" /></li>##2. 刪除任務
// vue 注冊點擊刪除事件 del(id) {this.$store.commit("del", { id }); }// vuex store // mutations const mutations = {del(state, playload) {let { id } = playloadstate.list = state.list.filter(v => v.id !== id)}, }##3. 添加任務
// vue<inputv-model="todoName" # ++@keyup.enter="addTodo" # ++class="new-todo"placeholder="What needs to be done?"autofocus/>data() {return {todoName: "" # ++};},methods: {addTodo() {this.$store.commit("add", {name: this.todoName});this.todoName = "";}}// vuex const mutations = {// 添加add(state, playload) {state.list.unshift({id: Date.now(),name: playload.name,done: false,})}, }##4. 修改任務
- 顯示編輯框
- 回車 - 修改數據
5. 修改狀態
// vue<inputclass="toggle"type="checkbox":checked="item.done"@change="iptChange(item.id,$event)"/> iptChange(id, e) {console.log(e.target.checked);this.$store.commit("iptChange", {id,checked: e.target.checked});}// vuex// 更新狀態iptChange(state, playload) {let { id, checked } = playloadlet todo = state.list.find(v => v.id === id)todo.done = checked # todo.done},##6. 計算屬性(三個)
// 計算屬性 const getters = {// 底部的顯示與隱藏isFooterShow(state) {return state.list.length > 0},// 剩余未完成數itemLeftCount(state) {return state.list.filter(v => !v.done).length},// 是否顯示清除已完成isClearCompletedShow(state) {let b = state.list.some(v => v.done)console.log(b)return state.list.some(v => v.done)}, }##7. 清除已經完成的任務
// vue<!-- 清除已完成 --><buttonclass="clear-completed"@click="$store.commit('clear')"v-show="$store.getters.isClearCompletedShow">Clear completed</button>// vuexclear(state) {state.list = state.list.filter(v => !v.done)},Action 的使用
- 官網介紹
- Action 類似于 mutation,不同在于:
- Action 可以包含任意異步操作。
- Action 提交的是 mutation,而不是直接變更狀態。
- mutaions 里不只能使用同步,不能出現異步 (演示刪除任務 里使用setTimeout 會報錯)
- 演示1: actions 可以包含任意異步操作。 代碼1
- 演示2: actions 不能直接變更狀態 , 代碼2 會報錯
- 演示3 : actions 提交的是 mutation
幾個輔助函數
1. mapState
當一個組件需要獲取多個狀態的時候,將這些狀態都聲明為計算屬性會有些重復和冗余。
我們可以使用 mapState 輔助函數 將 store 中的 state 映射到局部計算屬性
- 引入 mapState
- 數組形式
- **對象形式 **- 取個名字
2. mapGetters
mapGetters 輔助函數僅僅是將 store 中的 getter 映射到局部計算屬性
使用展開運算符將 getter 混入 computed 對象中
- 引入
- 數組形式
- 對象形式
如果你想將一個 getter 屬性另取一個名字,使用對象形式
computed: {// .... 之前 vue 的// 維護 vuex...mapGetters(["isFooterShow", "itemLeftCount"]),...mapGetters({// 把 `this.isShow` 映射為 `this.$store.getters.isClearCompletedShow`isShow: "isClearCompletedShow" // ==> 起名字 }) }// 使用 <button v-show="isShow">Clear completed</button>2. mapMutations
使用 mapMutations 輔助函數將組件中的 methods 映射為 store.commit 調用(需要在根節點注入 store)
- 引入
- 數組形式
- 對象形式 :
如果你想將一個 methods 方法另取一個名字,使用對象形式
methods: {// 將 this.del() 映射為 this.$store.commit('del')...mapMutations(["showEdit", "hideEdit", "iptChange"]),// 可以全部取名字 也可以改一個名字 // 將 this.d() 映射為 this.$store.commit('d') ...mapMutations({d: "del"}),del(id) {// this.$store.commit("del", { id });this.d({ id });}}3. mapActions
使用 mapActions 輔助函數將組件的 methods 映射為 store.dispatch 調用
- 引入
- 數組形式
- 對象形式
頭條-vuex-動態設置keep-alive
配置 vuex
const store = new Vuex.Store({state: {cachelist: ['home'],}, })- 使用
總結
以上是生活随笔為你收集整理的Vuex说明及Todos项目改造的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Promise 的基本使用 与 Ajax
- 下一篇: html5倒计时秒杀怎么做,vue 设