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ù)據(jù),寫法: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("恭喜您, 登錄成功! 即將跳轉(zhuǎn)到首頁");
// 路由跳轉(zhuǎn)指定頁面
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ù)據(jù),就需要使用action的dispatch去完成了,其他使用commit即可.
其他了解:
- commit => mutations, 用來觸發(fā)同步操作的方法.
- dispatch => actions, 用來觸發(fā)異步操作的方法.
在store中注冊了mutation和action
在組件中用dispatch調(diào)用action,用commit調(diào)用mutation
vue中Error in mounted hook: “TypeError: this.$store.$dispatch is not a function“ 報錯

這個錯誤屬于語法錯誤,初學者經(jīng)常犯的錯誤。
解決方法
將$dispatch改為dispatch
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue-Access-Control 前端用戶權限控制解決方案
Vue-Access-Control是一套基于Vue/Vue-Router/axios 實現(xiàn)的前端用戶權限控制解決方案。這篇文章主要介紹了Vue-Access-Control:前端用戶權限控制解決方案,需要的朋友可以參考下2017-12-12
Vue + Node.js + MongoDB圖片上傳組件實現(xiàn)圖片預覽和刪除功能詳解
這篇文章主要介紹了Vue + Node.js + MongoDB圖片上傳組件實現(xiàn)圖片預覽和刪除功能,結合實例形式詳細分析了Vue + Node.js + MongoDB基于圖片上傳組件實現(xiàn)圖片預覽和刪除功能相關操作技巧,需要的朋友可以參考下2020-04-04
vue基于html2canvas和jspdf?生成pdf?、解決jspdf中文亂碼問題的方法詳解
這篇文章主要介紹了vue基于html2canvas和jspdf?生成pdf?、解決jspdf中文亂碼問題的方法,結合實例形式詳細描述了中文亂碼問題的原因、解決方法與相關注意事項,需要的朋友可以參考下2023-06-06
vue項目打包解決靜態(tài)資源無法加載和路由加載無效(404)問題
這篇文章主要介紹了vue項目打包,解決靜態(tài)資源無法加載和路由加載無效(404)問題,靜態(tài)資源無法使用,那就說明項目打包后,圖片和其他靜態(tài)資源文件相對路徑不對,本文給大家介紹的非常詳細,需要的朋友跟隨小編一起看看吧2023-10-10

