4.归纳.md 2.3 KB

Axios

1.安装axios yarn add axios -g(全局) npm install axios -g -d(开发环境) -s (安装) 网络常见两种请求 get 方法 一般用于获取数据信息 post 方法 进行数据的传送 安全 put delete ...

Vuex

1.安装vuex npm install vuex yarn add vuex 2.在main.js中将状态管理库进行引入

vuex: 1.state 存放读取数据 在state对象中定义数据 在需要使用的地方 this.$store.state.xxx $store.state.xxx 引入mapState对象 在computed中进行结构:...mapState(["xxx","xxx",...])

2.getters 计算数据,类似于computed 在getters对象中定义函数方法 在需要使用的地方 this.$store.getters.xxx $store.getters.xxx 引入mapState对象 在computed中进行结构:...mapGetters(["xxx","xxx",...])

3.mutations 同步操作 定义方式与函数相同 使用方式: 1.this.$store.commit('调用的同步方法名称',传参) 2.引入mapMutations对象 在使用的位置前进行结构 ...mapMutations(["调用的同步方法名称"]) this.调用的同步方法名称();

4.Actions 异步操作 定义方式:通过commit获取到同步的方法 使用方式: this.$store.dispatch('调用的异步方法名称')

在使用的位置前进行结构 ...mapActions(["调用的异步方法名称"]) this.调用的异步方法名称();

通过状态(数据源)集中管理驱动组件的变化。页面通过mapActions异步提交事件到actions。actions通过commit把对应参数同步提交到mutations。

mutations会修改state中对于的值。 最后通过getters把对应值跑出去,在页面的计算属性中

通过mapGetters来动态获取state中的值

应用级的状态集中放在store中; 改变状态的方式是提交mutations,这是个同步的事物; 异步逻辑应该封装在actions中。

state中保存着共有数据,数据是响应式的 getters可以对state进行计算操作,主要用来过滤一些数据,可以在多组件之间复用 mutations定义的方法动态修改state中的数据,通过commit提交方法,方法必须是同步的 actions将mutations里面处理数据的方法变成异步的,就是异步操作数据,通store.dispatch来分发actions,把异步的方法写在actions中,通过commit提交mutations,进行修改数据。

modules:模块化vueX