Vue-Vuex

安装 Vuex 依赖包

//Vue2 需要指定安装Vuex3.0版本
npm i vuex@3.6.2

创建 store 文件夹

在 src 文件夹下新建 store 文件夹,然后在 store 文件夹中新建 index.js

index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state:{

  },
  mutations:{

  },
  actions:{
    
  }
})

main.js 中添加这两行

import store from './store'

new Vue({
  render: h => h(App),
  //添加store
  store
  
}).$mount('#app')

组件中访问 State 中数据的第一种方式

this.$store.state.全局数据名称

组件中访问 State 中数据的第二种方式

//从 vuex 中按需导入 mapState 函数
import { mapState } from 'vuex'

通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed 计算属性

//将全局数据,映射为当前组件的计算属性
computed:{
	...mapState([''])
}

Mutation

Mutation 用于变更 store 中的数据,只能通过 Mutation 变更 store 数据,不可以直接操作 store 中的数据。

export default new Vuex.Store({
  //共享的数据
  state: {
    count:0
  },
  //用于修改state中的数据
  mutations: {
    add(state){
      //变更状态
      state.count++
    }
  }
 methods:{
    btnadd(){
    //触发 mutations 的第一种方式
      this.$store.commit('add')
    }
  }

可以在触发 mutations 时传递参数

export default new Vuex.Store({
  //共享的数据
  state: {
    count:0
  },
  //用于修改state中的数据
  mutations: {
    addN(state,step){
      //变更状态
      state.count += step
    }
  }
export default {
  data() {
    return {

    }
  },
  methods:{
    btnadd(){
      //触发 mutations 时携带参数
      this.$store.commit('addN',3)
    }
  }
}

触发 mutations 的第二种方式

//从 vuex 中按需导入 mapMutation 函数
import { mapMutation } from 'vuex'

通过刚才导入的 mapMutation 函数,将需要的 mutation 函数,映射为当前组件的 methods 方法

import {mapState,mapMutations} from 'vuex'
methods:{
    ...mapMutations(['sub']),
    btnsub(){
      this.sub()
    },
  }

传参

mutations: {
    add(state,step){
      //变更状态
      state.count += step
    },
    sub(state){
      state.count --
    },
    subN(state,step){
      state.count -= step
    }
  },
methods:{
    ...mapMutations(['sub','subN']),
    btnsub(){
      this.sub()
    },
    btnsubN(){
      this.subN(2)
    }
  }

不要在mutation函数中,执行异步操作

Action

Action 用于处理异步任务

如果通过异步操作变更数据,必须通过 Action ,而不能使用 Mutation ,但是在 Action 中还是要通过触发 Mutation 的方式间接变更数据。

export default new Vuex.Store({
  //共享的数据
  state: {
    count:0
  },
  //用于修改state中的数据
  mutations: {
    add(state){
      //变更状态
      state.count ++
    },
    sub(state){
      state.count --
    },
    subN(state,step){
      state.count -= step
    }
  },
  actions: {
    addAsync(context){
      setTimeout(()=>{
        //通过context.commit触发mutation
        context.commit('add')
      },1000)
    }
  }
})
 methods:{
    //异步
    btnaddN(){
      this.$store.dispatch('addAsync')
    }
  }
}
触发 action 异步任务时携带参数
export default new Vuex.Store({
  //共享的数据
  state: {
    count:0
  },
  //用于修改state中的数据
  mutations: {
    add(state,step){
      //变更状态
      state.count += step
    },
    sub(state){
      state.count --
    },
    subN(state,step){
      state.count -= step
    }
  },
  actions: {
    addAsync(context,step){
      setTimeout(()=>{
        //通过context.commit触发mutation
        context.commit('add',step)
      },1000)
    }
  }
})
methods:{
    btnadd(){
      //触发 mutations 时携带参数
      this.$store.commit('add')
    },
    //异步携带参数
    btnaddN(){
      this.$store.dispatch('addAsync',3)
    }
  }

this.$store.dispatch() 是触发 actions 的第一种方式,触发 actions 的第二种方式:

//从 vuex 中按需导入 mapActions 函数
import { mapActions } from 'vuex'

通过刚才的mapActions 函数,将需要的 actions 函数,映射为当前组件的 methods 方法

methods:{
...mapActions(['addAsync','addNAsync']),
	btnsub1(){
      this.subAsync()
    }
}

Getter

Getter 用于对 Store 中的数据进行加工处理形成新的数据

Store 中数据发生变化,Getter的数据也会跟着变化

export default new Vuex.Store({
  //共享的数据
  state: {
    count:0
  },
  //用于修改state中的数据
  mutations: {
    add(state,step){
      //变更状态
      state.count += step
    },
    sub(state){
      state.count --
    },
    subN(state,step){
      state.count -= step
    }
  },
  actions: {
    addAsync(context,step){
      setTimeout(()=>{
        //通过context.commit触发mutation
        context.commit('add',step)
      },1000)
    },
    subAsync(context){
      setTimeout(()=>{
        context.commit('sub')
      },1000)
    }
  },
  getters:{
    showNum(state){
      return `当前最新数量:${state.count}`
    }
  }
})

使用Getters 的第一种方式:

this.$store.getters.名称

使用Getters 的第二种方式:

import { mapGetters } from 'vuex'

computed:{
	...mapGetters(['showNum'])
}
<h3>{{ showNum }}</h3>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋枫 ~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值