网站首页 > 技术文章 正文
问题:组件中使用 vuex 的值和修改值的地方?
以 8.3小节中vuex设置的值和方法为例,在 .vue 组件中使用:
直接获取、修改:
//state this.$store.state.count //getter this.$store.getters.count //调用 action 修改 state 值,不带参数 this.$store.dispatch('increment'); //调用 action 修改 state 值,带参数 this.$store.dispatch('increment',{value :123});
通过 辅助函数 获取、修改vuex:
vuex提供了三种辅助函数用于获取、修改vuex: mapState、mapGetters、mapActions 即将vuex的变量或者方法映射到vue组件this指针上。
获取共享变量使用:
//使用state获取共享变量 <script type="text/javascript"> import { mapState } from 'vuex' export default{ computed : { ...mapState([ 'count', 'buttonShow' ]) } } </script> //使用getters获取共享变量, <script type="text/javascript"> import { mapGetters } from 'vuex' export default{ computed : { ...mapGetters([ 'count', 'buttonShow' ]) } } </script> //使用actions修改共享变量 <script type="text/javascript"> import { mapActions } from 'vuex' export default{ methods : { ...mapActions({increment:'increment',decrement:'decrement'}), } } </script>
通过map获取到的变量或者方法,可通过 this 直接使用或者调用。
编辑器:
欢迎关注,有问题请咨询。
猜你喜欢
- 2024-10-02 Vue3,Vuex,集中式状态(数据)管理,四个模块:state、getter
- 2024-10-02 Netflix 开源危机管理工具 Dispatch,真香
- 2024-10-02 Vuex基本使用(vuex的基本使用)
- 2024-10-02 vue组件间传递参数的几种方式(vue组件之间的参数传递)
- 2024-10-02 vue前端实现与安卓和ios之间的通信
- 2024-10-02 2024前端面试题(三)(2021前端最新面试题)
- 2024-10-02 Vue.js应用的四种AJAX请求数据模式以及优缺点
- 2024-10-02 总结4个方面优化Vue项目(如何优化vue项目)
- 2024-10-02 Node + Express + Mysql + Vue 全栈开发项目:Todo List
- 2024-10-02 Vuex使用指南-Actions(三)(vuex的使用流程)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)