助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
# vuex的基本使用 概念:是一个专为 Vue.js 应用程序开发的**状态管理模式**。这里主要是用来方便组件之间联系 **注意 : 1.vuex不允许直接在组件中修改store的数据 ,如:this.$store.state.count++ 2.mutations函数中不能写异步的代码,如:setTimeOut方法** ## 使用: ``` 获取数据: 1.this.$store.store.xxx 2.计算属性:通过引入辅助函数(mapStore),在计算属性中获取数据 触发方法: 一、同步的方法: 1.this.$store.commit('xxx') 2.通过引入辅助函数(mapMutation),在methods中获取触发函数 二、异步的方法 1.this.$store.dispatch('xxx') 2. 通过引入辅助函数(mapAction),在methods中获取触发函数 ``` #### 在main.js中: **~~不推荐直接在main中引入~~** ``` 安装: npm install vuex --save 导入: import Vuex from 'vuex' Vue.use(Vuex) ``` ``` 具体使用: 1.新建一个store文件夹,再键一个index.js 2.导入vue和vuex:import xx form “xx” 创建store对象: const store = new Vuex.Store({ //state中存放的就是全局共享的数据 state: { count: 0 }, mutations: { increment (state) { state.count++ } } }) 导出:export defult store ``` ``` 导入store:import store from “./store” 将store对象挂载到vue实例中: newVue({ el:'#app', router, store, //store对象,挂载到vue实例中,所有的组件就可以直接从store中获取全局数据了 render:h=>h(App) }) ``` ## 核心概念 1.State : 提供唯一的数据源,所有共享的数据都要统一放到Store的State中进行存储 ① 组件访问数据的第一种方式:this.$store.state.count (count是state中定义的数据对象中的数据) ② 组件访问数据的第二种方式 :通过mapState辅助函数获取数据,先在组件中引入: import {mapStore} from ‘vuex’, 通过计算属性,将数据映射到组件中使用:computed:{ ...mapState(['count'])},此时count在组件中就能使用了,如果这个变量名跟data中有冲突,可以用对象形式:computed:{ ...mapState({"count1" : 'count'})} 2.Mutation : 用来更改state数据的状态(修改数据),可以集中监控数据的变化 ``` //定义mutations const store = new Vuex.Store({ state: { count: 1 }, mutations: { //定义事件对象,处理数据和逻辑,state就是state对象,作为形参调用 add(state) { // 变更状态 state.count++ } } }) ``` ① 在组件中触发mutation 的方式一: a. ``` methods : { handle1( ){ this.$store.commit("add") //add是方法名,通过commit触发 } } ``` b. 在组件中触发mutation 并传递参数的方式: ``` //定义mutations const store = new Vuex.Store({ state: { count: 1 }, mutations: { //定义事件对象,处理数据和逻辑,state就是state对象,作为形参调用,step为传递的参数 addN(state,step) { // 变更状态 state.count += step } } }) 组件中传递参数并使用: methods : { handle1( ){ this.$store.commit("addN" , 3) //add是方法名,通过commit触发,第二个参数为组件传递的参数 } } ``` ② 触发mutation的第二种方式: 在组件中按需导入mapMutations函数:`import { mapMutations } from 'vuex'` 通过刚才导入的mapMutation函数,将需要的mutations函数,映射为当前组件的methods方法中:`methods : { ...mapMutations( [ 'add','addN'] )}` ``` //定义mutations const store = new Vuex.Store({ state: { count: 1 }, mutations: { //定义事件对象,处理数据和逻辑,state就是state对象,作为形参调用,step为传递的参数 addN(state,step) { // 变更状态 state.count += step }, sub(state){ state.count-- } } }) 组件中使用: methods : { //将sub方法映射到组件中使用 ...mapMutations(['sub']), handle2( ){ //调用sub方法 this.sub( ) } } ``` **携带参数用法和上面一致,添加第二个参数即可** ### 3.Action:用于处理异步任务 ① 触发actions的第一种方法 : ``` //定义actions const store = new Vuex.Store({ state: { count: 1 }, mutations: { //定义事件对象,处理数据和逻辑,state就是state对象,作为形参调用,step为传递的参数 addN(state,step) { // 变更状态 state.count += step }, sub(state){ state.count-- } }, actions : { //处理异步操作,1s后执行add方法 addAsync(context){ setTimeOut( ( )=>{ context.commit('add') },1000) } } }) 组件中触发Action: methods : { handle( ){ //触发actions的第一种方式,dispatch方法 this.$store.dispatch('addAsync') } } **如果需要携带参数,像上面方法一样,添加第二个参数即可 ``` ② 触发actions的第二种方法 : 在组件中导入mapActions:`import { mapActions from ‘vuex’` ``` 在组件中 : methods : { ...mapActions([ 'addAsync']) // 将方法映射到组件中 //通过事件调用 handle(){ this.addAsync( ) } } ``` ## 4.Getter : 用户对store中的数据进行加工处理形成新的数据 Getter可以对Store中已有的数据加工处理之后形成新的数据,类似vue的计算属性 store中的数据发生变化,Getter中的数据也会跟着变化 ``` 定义Getter 创建store对象: const store = new Vuex.Store({ //state中存放的就是全局共享的数据 state: { count: 0 }, getters : { showNum : state =>{ return '当前的数量是【'+ state.count +'】'} } } }) ``` 在组件中使用getter方法一 : ``` this.$store.getters.showNum(定义的名称) ``` 在组件中使用getter方法二 : 在组件中引入mapGetters : `import {mapGetters} from 'vuex'` 在组件计算属性中:` computed : {...mapGetter( ['showNum'] )}` //把属性映射到组件中使用 :{{showNum}}