vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說明
this.$store.dispatch()與this.$store.commit()的區(qū)別及說明
this.$store.dispatch() 與 this.$store.commit()方法的區(qū)別總的來說他們只是存取方式的不同,兩個方法都是傳值給vuex的mutation改變state
this.$store.dispatch()
:含有異步操作,例如向后臺提交數(shù)據,寫法:this.$store.dispatch(‘action方法名’,值)this.$store.commit()
:同步操作,,寫法:this.$store.commit(‘mutations方法名’,值)
commit: 同步操作
存儲 this.$store.commit('changeValue',name)
取值 this.$store.state.changeValue
dispatch: 異步操作
存儲 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 { // 官方推薦: 給組件起個名字, 便于報錯時的提示 name: "Header", // 引入vuex 的 store 中的state值, 必須在計算屬性中書寫! 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("恭喜您, 登錄成功! 即將跳轉到首頁"); // 路由跳轉指定頁面 this.$router.push({ path: "/" }); //更新 vuex 的 state的值, 必須通過 mutations 提供的方法才可以 // 通過 commit('方法名') 就可以出發(fā) mutations 中的指定方法 this.$store.commit("doLogin"); } else { alert("很遺憾, 登陸失敗!"); } }) .catch(err => { console.error(err); }); } } }; </script>
兩個方法其實很相似,關鍵在于一個是同步,一個是異步
commit: 同步操作
this.$store.commit('方法名',值) //存儲 this.$store.state.'方法名' //取值
dispatch: 異步操作
this.$store.dispatch('方法名',值) //存儲 this.$store.getters.'方法名' //取值
當操作行為中含有異步操作,比如向后臺發(fā)送請求獲取數(shù)據,就需要使用action的dispatch去完成了,其他使用commit即可.
其他了解:
- commit => mutations, 用來觸發(fā)同步操作的方法.
- dispatch => actions, 用來觸發(fā)異步操作的方法.
在store中注冊了mutation和action
在組件中用dispatch調用action,用commit調用mutation
vue中Error in mounted hook: “TypeError: this.$store.$dispatch is not a function“ 報錯
這個錯誤屬于語法錯誤,初學者經常犯的錯誤。
解決方法
將$dispatch改為dispatch
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue2.0用 watch 觀察 prop 變化(不觸發(fā))
本篇文章主要介紹了Vue2.0用 watch 觀察 prop 變化(不觸發(fā)),非常具有實用價值,需要的朋友可以參考下2017-09-09electron-vue?項目添加啟動loading動畫的實現(xiàn)思路
electron-vue腳手架搭建的項目,在開發(fā)階段可能你注意不到項目啟動慢的問題,但是在build?生成的exe可執(zhí)行文件,啟動后,要反應很久才能進入到app.vue?中加載的頁面,體驗性很差,本文給大家介紹electron?vue啟動動畫效果的實例代碼,感興趣的朋友一起看看吧2022-01-01vue3實現(xiàn)封裝時間計算-日期倒計時組件-還有XX天&第XX天
這篇文章主要介紹了vue3實現(xiàn)封裝時間計算-日期倒計時組件-還有XX天&第XX天,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03在VUE中使用lodash的debounce和throttle操作
這篇文章主要介紹了在VUE中使用lodash的debounce和throttle操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11