vuex中 this.$store.dispatch() 与 this.$store.commit()方法的区别
this.$store.dispatch() 与 this.$store.commit()方法的区别总的来说他们只是存取方式的不同,两个方法都是传值给vuex的mutation改变state
this.$store.dispatch() :含有异步操作,例如向后台提交数据,写法: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专门用来保存 共享的状态值
state: {
// 保存登录状态
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辅助函数, 可以快速引入store中的值
// 此处的login代表, store文件中的 state 中的 login, 登录状态
...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('方法名') 就可以出发 mutations 中的指定方法
this.$store.commit("doLogin");
} else {
alert("很遗憾, 登陆失败!");
}
})
.catch(err => {
console.error(err);
});
}
}
};
</script>
猜您可能还喜欢
- CERT_HAS_EXPIRED错误如何解决(2951)
- Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2591)
- Vue 2.x + Element后台模板开发教程(三)后台首页模板设计(1288)
- vuex中 this.$store.dispatch() 与 this.$store.commit()方法的区别(1263)
- Error: Cannot find module ‘chokidar‘ 解决方法(1254)
- Vue后台开发实例教程(二)Title修改及左侧菜单修改(1145)
- Vue 2.x + Element后台模板开发教程(一)(1110)
- vuejs中如何使用axios调用后台接口(1110)
- vue.js初探(一)vue.js全家桶介绍(908)
- vue.js初探(三)vue页面结构(903)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5871)
- 2.Windows 10休眠文件更改存储位置(3794)
- 3.各大搜索网站网站收录提交入口地址(3476)
- 4.ECharts仪表盘实例及参数使用详解(3424)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(3417)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(3170)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(3154)
- 8.CERT_HAS_EXPIRED错误如何解决(2951)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2591)
- 10.HBuilder编辑器格式化代码(2385)
