Vue唯一可以更改vuex實(shí)例中state數(shù)據(jù)狀態(tài)的屬性對(duì)象Mutation的講解
更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation。
Vuex 中的 mutation 非常類似于事件:
每個(gè) mutation 都有一個(gè)字符串的 事件類型 (type) 和 一個(gè) 回調(diào)函數(shù) (handler)。這個(gè)回調(diào)函數(shù)就是我們實(shí)際進(jìn)行狀態(tài)更改的地方,并且它會(huì)接受 state 作為第一個(gè)參數(shù)。mutation 必須是同步函數(shù)。
不帶載荷(只改變數(shù)據(jù)的狀態(tài))
接前面幾篇文章的例子,這里我們修改商品價(jià)格減半。
store.js
mutations: { //商品價(jià)格減半;更改這個(gè)數(shù)據(jù)狀態(tài)必須將這個(gè)數(shù)據(jù)源state傳遞過來 goodsPriceHalve: state => { let goodsPriceHalve = state.goodsList.map(currentValue => { return { name: currentValue.name, price: currentValue.price/2 } }) state.goodsList = goodsPriceHalve; } }
page5.vue
要喚醒一個(gè) mutation handler,你需要以相應(yīng)的 type(事件) 調(diào)用 store.commit 方法。
<template> <div> <h2>我是第三個(gè)頁面</h2> <!-- 直接在HTML標(biāo)簽中使用 --> <div>{{$store.state.goodsList}}</div> <br> <router-link to='/page6'>更改商品名字</router-link> <br> <button @click="handleGoodsHavle">商品價(jià)格減半</button> <ul class="ul_list"> <li v-for="item in goodsListHalv"> <p class="name">商品:{{item.name}}</p> <p class="price">價(jià)格:¥{{item.price}}</p> </li> </ul> </div> </template> <script> export default { data() { return { /* // mutations不能通過直接賦值的形式改變state的數(shù)據(jù)狀態(tài) goodsListHalv: this.$store.state.goodsList, */ // goodsListHalv: [] } }, /* // mutations不能通過鉤子函數(shù)的形式進(jìn)行賦值 mounted(){ this.goodsListHalv = this.$store.state.goodsList }, */ // 通過計(jì)算屬性的方式,是完美的 computed: { goodsListHalv(){ return this.$store.state.goodsList; } }, methods: { handleGoodsHavle: function(){ //這里只通過事件改變數(shù)據(jù)的狀態(tài) this.$store.commit('goodsPriceHalve') } } } </script>
效果圖
提交載荷(Payload)(改變數(shù)據(jù)狀態(tài)的同時(shí)傳遞參數(shù))
參數(shù):字符串/對(duì)象
這里修改商品名字。
store.js
// 通過組件上的事件,通過this.$store.commit('mutations中的函數(shù)','需要從組件上傳遞給 mutation中這個(gè)函數(shù)的參數(shù)') mutations: { // 統(tǒng)一修改商品的名稱;changeName(state,payload)參數(shù)1 state:數(shù)據(jù)源,參數(shù)2 payload:接收的參數(shù) changeName: (state,payload) => { var changeName = state.goodsList.map(currentValue => { return { name: payload,//接收參數(shù) price: currentValue.price/2 } }) state.goodsList = changeName; } }
這里的載荷payload可以是一個(gè)對(duì)象/字符串。
page6.vue
<template> <div> <h2>我是第四個(gè)頁面</h2> <div> <input type="text" placeholder="請(qǐng)輸入商品的新名字" v-model="inpValue"> <button @click="changeGoodsName()">商品價(jià)格減半</button> </div> <router-link to='/page7'>action</router-link> <ul class="ul_list"> <li v-for="item in goodsListHalv"> <p class="name">商品:{{item.name}}</p> <p class="price">價(jià)格:¥{{item.price}}</p> </li> </ul> </div> </template> <script> export default { data() { return { inpValue:'', } }, computed: { goodsListHalv(){ return this.$store.state.goodsList; } }, methods: { // 通過 click事件觸發(fā)methods中的函數(shù),進(jìn)而改變store.js中數(shù)據(jù)的狀態(tài) changeGoodsName: function(){ // this.$store.commit('需要操作mutations中的函數(shù)名','從這個(gè)組件傳遞給第一個(gè)參數(shù)的參數(shù)') this.$store.commit('changeName',this.inpValue) } } } </script>
這里的載荷payload就是輸入框的值。
效果圖
代碼執(zhí)行過程
上面的Mutation執(zhí)行過程是:按鈕點(diǎn)擊–>執(zhí)行組件中按鈕點(diǎn)擊事件方法–>執(zhí)行$store.commit('vuex中mutatioms對(duì)象中對(duì)應(yīng)的函數(shù)名',需要傳遞的參數(shù))–>執(zhí)行mutations里面對(duì)應(yīng)的方法–>修改state里面對(duì)應(yīng)的數(shù)據(jù)–>頁面渲染。
Mutation 需遵守 Vue 的響應(yīng)規(guī)則
既然 Vuex 的 store 中的狀態(tài)是響應(yīng)式的,那么當(dāng)我們變更狀態(tài)時(shí),監(jiān)視狀態(tài)的 Vue 組件也會(huì)自動(dòng)更新。這也意味著 Vuex 中的 mutation 也需要與使用 Vue 一樣遵守一些注意事項(xiàng):
最好提前在你的 store 中初始化好所有所需屬性。當(dāng)需要在對(duì)象上添加新屬性時(shí),你應(yīng)該使用 Vue.set(obj, 'newProp', 123), 或者以新對(duì)象替換老對(duì)象。例如,利用 stage-3 的對(duì)象展開運(yùn)算符我們可以這樣寫:
state.obj = { ...state.obj, newProp: 123 }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Vue+Java+Base64實(shí)現(xiàn)條碼解析的示例
這篇文章主要介紹了Vue+Java+Base64實(shí)現(xiàn)條碼解析的示例,幫助大家實(shí)現(xiàn)條碼解析,感興趣的朋友可以了解下2020-09-09Vue.js組件props數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Vue.js組件props數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10vue Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格
這篇文章主要為大家詳細(xì)介紹了vue Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06vue3+vite使用環(huán)境變量.env的一些配置情況詳細(xì)說明
開發(fā)中經(jīng)常會(huì)使用環(huán)境變量,Vite相比于Webpack也有一定的變化,下面這篇文章主要給大家介紹了關(guān)于vue3+vite使用環(huán)境變量.env的一些配置情況說明的相關(guān)資料,需要的朋友可以參考下2022-12-12解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題
這篇文章主要介紹了解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10