wordpress建站程序,广告营销是做什么的,企业微网站哪个版本好,甬城通可以在线充值吗1.概述
Vuex是实现组件全局状态#xff08;数据#xff09;管理的一种机制#xff0c;可以方便的实现组件之间的数据共享
优点#xff1a;
能够在vuex中集中管理共享的数据#xff0c;便于开发和后期进行维护能够高效的实现组件之间的数据共享#xff0c;提高开发效率…1.概述
Vuex是实现组件全局状态数据管理的一种机制可以方便的实现组件之间的数据共享
优点
能够在vuex中集中管理共享的数据便于开发和后期进行维护能够高效的实现组件之间的数据共享提高开发效率存储在vuex中的数据是响应式的当数据发生改变时页面中的数据也会同步更新
2.Vuex的基本使用步骤 安装 npm i vuex --save 在src文件目录下新建storeindex.js文件 import Vue from vue;
import Vuex from vuex;
Vue.use(Vuex);
const store new Vuex.Store();export default store;入口文件里面引入store然后再全局注入 import store from ./store//引入storenew Vue({el: #app,router,store,//使用storetemplate: App/,components: { App }
})使用 4.1 在state中定义数据 Vue.use(Vuex)
const store new Vuex.Store({state:{count:1}
})4.2 Getter相当于vue中的computed计算属性getter 的返回值会根据它的依赖被缓存起来且只有当它的依赖值发生了改变才会被重新计算Getters 可以用于监听、state中的值的变化返回计算后的结果 getters:{getCount:state{return state.count1}4.3 给action注册事件处理函数当这个函数被触发时候将状态提交到mutations中处理。actions里面自定义的函数接收一个context参数和要变化的形参 actions:{addFun(context,n){context.commit(add,n)} removeFun(context){context.commit(remove)}}4.4 mutations是一个对象里。面的方法 都是同步事务是更改state初始状态的唯一合法方法具体的用法就是给里面的方法传入参数state或额外的参数 mutations:{addstaten){state.count state.countn},remove(){state.countstate.count-1}},4.5 dispatch含有异步操作例如向后台提交数据写法 this.$store.dispatch(‘action方法名’,值) commit同步操作写法this.$store.commit(‘mutations方法名’,值) export defult{data(){return{mag:aaa}},methods:{addCount(){this.$store.commit(add)},reoveCount:function(){this.$store.commit(remove)},addFun(){let n 2;this.$store.dispatch(addFun,n)},removeFun(){this.$store.dispatch(removeFun)}}
}3. Vuex中的核心特性
3.1 State
概述 State提供唯一的公共数据源所有共享的数据都要统一放到Store中的State中存储
在组件中访问State的方式 方式一
this.$store.state.全局数据名称 如this.$store.state.count方式二
先按需导入mapState函数 import { mapState } from vuex
然后数据映射为计算属性 computed:{ ...mapState([全局数据名称]) }3.2. Mutation
概述 Mutation用于修改变更$store中的数据
使用方式第一种 打开store.js文件在mutations中添加代码如下
mutations: {add(state,step){//第一个形参永远都是state也就是$state对象//第二个形参是调用add时传递的参数state.countstep;}}然后在Addition.vue中给按钮添加事件代码如下
button clickAdd1/buttonmethods:{Add(){//使用commit函数调用mutations中的对应函数//第一个参数就是我们要调用的mutations中的函数名//第二个参数就是传递给add函数的参数this.$store.commit(add,10)}
}使用mutations的第二种方式
import { mapMutations } from vuexmethods:{...mapMutations([add])
}
import { mapState,mapMutations } from vuexexport default {data() {return {}},methods:{//获得mapMutations映射的sub函数...mapMutations([sub]),//当点击按钮时触发Sub函数Sub(){//调用sub函数完成对数据的操作this.sub(10);}},computed:{...mapState([count])}
}3.3 Action
概述 在mutations中不能编写异步的代码会导致vue调试器的显示出错。 在vuex中我们可以使用Action来执行异步操作。
操作步骤如下 打开store.js文件修改Action如下
actions: {addAsync(context,step){setTimeout((){context.commit(add,step);},2000)}
}然后在Addition.vue中给按钮添加事件代码如下
button clickAddAsync...1/buttonmethods:{AddAsync(){this.$store.dispatch(addAsync,5)}
}第二种方式
import { mapActions } from vuexmethods:{...mapMutations([subAsync])
}
import { mapState,mapMutations,mapActions } from vuexexport default {data() {return {}},methods:{//获得mapMutations映射的sub函数...mapMutations([sub]),//当点击按钮时触发Sub函数Sub(){//调用sub函数完成对数据的操作this.sub(10);},//获得mapActions映射的addAsync函数...mapActions([subAsync]),asyncSub(){this.subAsync(5);}},computed:{...mapState([count])}
}3.4 Getter
概述 Getter用于对Store中的数据进行加工处理形成新的数据 它只会包装Store中保存的数据并不会修改Store中保存的数据当Store中的数据发生变化时Getter生成的内容也会随之变化
打开store.js文件添加getters如下
export default new Vuex.Store({.......getters:{//添加了一个showNum的属性showNum : state {return 最新的count值为state.count;}}
})然后打开Addition.vue中添加插值表达式使用getters
或者也可以在Addition.vue中导入mapGetters并将之映射为计算属性
import { mapGetters } from vuex
computed:{...mapGetters([showNum])
}