vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說明
this.$store.dispatch()與this.$store.commit()的區(qū)別及說明
this.$store.dispatch() 與 this.$store.commit()方法的區(qū)別總的來說他們只是存取方式的不同,兩個(gè)方法都是傳值給vuex的mutation改變state
this.$store.dispatch()
:含有異步操作,例如向后臺(tái)提交數(shù)據(jù),寫法:this.$store.dispatch(‘action方法名’,值)this.$store.commit()
:同步操作,,寫法:this.$store.commit(‘mutations方法名’,值)
commit: 同步操作
存儲(chǔ) this.$store.commit('changeValue',name)
取值 this.$store.state.changeValue
dispatch: 異步操作
存儲(chǔ) this.$store.dispatch('getlists',name)
取值 this.$store.getters.getlists
案例:
Vuex文件 src/store/index.js
import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); export const store = new Vuex.Store({ // state 共享的狀態(tài)值 state: { // 保存登錄狀態(tài) login: false }, // mutations: 專門書寫方法,用來更新 state 中的值 mutations: { // 登錄方法 doLogin(state) { state.login = true; }, // 退出方法 doLogout(state) { state.login = false; } } });
組件JS部分 : src/components/Header.vue
<script> // 使用vux的 mapState需要引入 import { mapState } from "vuex"; export default { // 官方推薦: 給組件起個(gè)名字, 便于報(bào)錯(cuò)時(shí)的提示 name: "Header", // 引入vuex 的 store 中的state值, 必須在計(jì)算屬性中書寫! computed: { // mapState輔助函數(shù), 可以快速引入store中的值 // 此處的login代表, store文件中的 state 中的 login, 登錄狀態(tài) ...mapState(["login"]) }, methods: { logout() { this.$store.commit("doLogout"); } } }; </script>
組件JS部分 : src/components/Login.vue
<script> export default { name: "Login", data() { return { uname: "", upwd: "" }; }, methods: { doLogin() { console.log(this.uname, this.upwd); let data={ uname:this.uname, upwd:this.upwd } this.axios .post("user_login.php", data) .then(res => { console.log(res); let code = res.data.code; if (code == 1) { alert("恭喜您, 登錄成功! 即將跳轉(zhuǎn)到首頁(yè)"); // 路由跳轉(zhuǎn)指定頁(yè)面 this.$router.push({ path: "/" }); //更新 vuex 的 state的值, 必須通過 mutations 提供的方法才可以 // 通過 commit('方法名') 就可以出發(fā) mutations 中的指定方法 this.$store.commit("doLogin"); } else { alert("很遺憾, 登陸失敗!"); } }) .catch(err => { console.error(err); }); } } }; </script>
兩個(gè)方法其實(shí)很相似,關(guān)鍵在于一個(gè)是同步,一個(gè)是異步
commit: 同步操作
this.$store.commit('方法名',值) //存儲(chǔ) this.$store.state.'方法名' //取值
dispatch: 異步操作
this.$store.dispatch('方法名',值) //存儲(chǔ) this.$store.getters.'方法名' //取值
當(dāng)操作行為中含有異步操作,比如向后臺(tái)發(fā)送請(qǐng)求獲取數(shù)據(jù),就需要使用action的dispatch去完成了,其他使用commit即可.
其他了解:
- commit => mutations, 用來觸發(fā)同步操作的方法.
- dispatch => actions, 用來觸發(fā)異步操作的方法.
在store中注冊(cè)了mutation和action
在組件中用dispatch調(diào)用action,用commit調(diào)用mutation
vue中Error in mounted hook: “TypeError: this.$store.$dispatch is not a function“ 報(bào)錯(cuò)
這個(gè)錯(cuò)誤屬于語法錯(cuò)誤,初學(xué)者經(jīng)常犯的錯(cuò)誤。
解決方法
將$dispatch改為dispatch
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)移動(dòng)端input上傳視頻、音頻
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端input上傳視頻、音頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Vue2.0用 watch 觀察 prop 變化(不觸發(fā))
本篇文章主要介紹了Vue2.0用 watch 觀察 prop 變化(不觸發(fā)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09electron-vue?項(xiàng)目添加啟動(dòng)loading動(dòng)畫的實(shí)現(xiàn)思路
electron-vue腳手架搭建的項(xiàng)目,在開發(fā)階段可能你注意不到項(xiàng)目啟動(dòng)慢的問題,但是在build?生成的exe可執(zhí)行文件,啟動(dòng)后,要反應(yīng)很久才能進(jìn)入到app.vue?中加載的頁(yè)面,體驗(yàn)性很差,本文給大家介紹electron?vue啟動(dòng)動(dòng)畫效果的實(shí)例代碼,感興趣的朋友一起看看吧2022-01-01vue內(nèi)嵌iframe跨域通信的實(shí)例代碼
這篇文章主要介紹了vue內(nèi)嵌iframe跨域通信,主要介紹了Vue組件中如何引入iframe,vue如何獲取iframe對(duì)象以及iframe內(nèi)的window對(duì)象,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì)需要的朋友可以參考下2022-11-11vue3實(shí)現(xiàn)封裝時(shí)間計(jì)算-日期倒計(jì)時(shí)組件-還有XX天&第XX天
這篇文章主要介紹了vue3實(shí)現(xiàn)封裝時(shí)間計(jì)算-日期倒計(jì)時(shí)組件-還有XX天&第XX天,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue讀取本地的excel文件并顯示在網(wǎng)頁(yè)上方法示例
這篇文章主要介紹了vue讀取本地的excel文件并顯示在網(wǎng)頁(yè)上方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05在VUE中使用lodash的debounce和throttle操作
這篇文章主要介紹了在VUE中使用lodash的debounce和throttle操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11