博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vuex 的使用
阅读量:7104 次
发布时间:2019-06-28

本文共 1331 字,大约阅读时间需要 4 分钟。

开启严格模式(!!!不要在发布环境下启用严格模式

开启严格模式,store 的 strict 属性设置为 true: const store = new Vuex.Store({  // ...  strict: true}); 严格模式下的状态发生变化且不是由mutation触发,将会抛出错误。保障所有状态的改变会被调试工具检测到。仅在开发环境下使用严格模式,生产环境下应关闭严格模式,减少性能的消耗。 使用构建工具如下配置(开发环境下使用严格模式,生产环境下切换到非严格模式):
const store = new Vuex.Store({  // ...  strict:process.env.NODE_ENV !== 'production' });
 

 

Action

1、Action 用于提交 mutation,修改state的值,而非直接修改;2、Action 用于处理各种异步操作的方法 const store = new Vuex.Store({
state: { list: [],    name:'', }, mutations: { editList(state , params) { state.list = params; },   editName(state , params){     state.name = params;   } }, actions: { actEditList(context) { context.commit('eidtList' , [1,2,3]); },   getInfo({ commit },{ name }){     commit('eidtName',name);   } } }); 3、Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象( store 的镜像,而非 store 本身),可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。 4、Action 函数的参数可使用 ES2015 的解构赋值语法:
actions: { actEditList( {commit} ) { commit('eidtList' , [1,2,3]); } } 5、组件中使用 Action
import { mapActions } from 'vuex'
export default{
     methods:{
    ...mapActions([       'actEditList',  // 在此组件中可使用 this.actEditList() 调用;     ]),     ...mapActions({
      get:'getInfo',  // 在此组件中可使用 this.get() 调用;     }),   }, }
 

 

转载于:https://www.cnblogs.com/zhaoxiaoying/p/10595262.html

你可能感兴趣的文章
开发指南专题八:JEECG微云高速开发平台数据字典
查看>>
CI框架 -- 核心文件 之 Output.php(输出类文件)
查看>>
动态更换view类的背景----StateListDrawable的应用
查看>>
scrapy-redis实现爬虫分布式爬取分析与实现
查看>>
Android仿微信UI布局视图(圆角布局的实现)
查看>>
docker
查看>>
OKR 方法 学习笔记
查看>>
CG资源网 - Maya教程
查看>>
http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
查看>>
CSS默认可继承样式
查看>>
数据库中树形结构的表的设计
查看>>
关于Cocos2d-x的瓦片地图
查看>>
位置无关码
查看>>
find-k-pairs-with-smallest-sums
查看>>
情绪板携手视觉设计
查看>>
Atitit.php nginx页面空白 并返回500的解决
查看>>
http://blog.csdn.net/LANGXINLEN/article/details/50421988
查看>>
PHP高效率写法(详解原因)
查看>>
Swift 值类型/引用类型
查看>>
【WPF】点击滑动条(Slider),移动滑块(Tick)到鼠标点击的位置
查看>>