Vue之Vuex的getters和actions的使用

继前文《Vue之Vuex(状态管理模式)》,在store.js添加getters和actions方法:

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

Vue.use(Vuex)

const store =  new Vuex.Store({
  state:{
    count:1
  },
  mutations:{
    increment(state){
      state.count++
    }
  },
  getters:{
    gettersCount(state){
      return state.count * 2
    }
  },
  actions:{
    actionsCount(context){
      //调用mutations的自增方法
      context.commit('increment')
    }
  }
})

export default store

在About.vue访问新增的方法,this. s t o r e . g e t t e r s . g e t t e r s C o u n t 访 问 s t o r e . j s 的 g e t t e r s , t h i s . store.getters.gettersCount访问store.js的getters, this. store.getters.gettersCount访store.jsgettersthis.store.dispatch(‘actionsCount’)调用store.js的actions定义好的actionsCount,先访问actionsCount,在实现mutations里的自增方法:

<template>
  <div class="about">
    这是about组件
    <br>
    <br>
    <br>
    <br>
    <!-- 3.引用store的count -->
    {{this.$store.state.count}}---{{this.$store.getters.gettersCount}}
    <button  @click="incre">累加</button>
  </div>
</template>

<script>
//1.引入store
import store from "../store.js"
export default {
  data(){
    return{
      title:"这是about组件"
    }
  },
  //2.注册
  store,
  methods:{
    incre(){
      //改变值的话,引用store.js的increment方法
      //this.$store.commit("increment")
      this.$store.dispatch('actionsCount')
    }
  }
}
</script>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Vue2中的Vuex的步骤: 1. 首先,确保你已经安装了VueVuex。你可以使用以下命令来安装它们: ```shell npm install vue npm install vuex ``` 2. 创建一个Vuex store。在你的项目中,创建一个新的JavaScript文件,例如store.js,并在其中导入VueVuex,并创建一个新的Vuex store实例。在store.js文件中,你可以定义state、mutations、actions和getters等属性和方法。例如: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ } }, actions: { increment(context) { context.commit('increment') } }, getters: { getCount(state) { return state.count } } }) export default store ``` 3. 在你的Vue组件中使用Vuex。在你的Vue组件中,你可以通过导入store.js文件并使用Vue的`$store`属性来访问Vuex store中的状态和方法。例如,在你的组件中使用`$store.state`来访问state中的数据,使用`$store.commit`来调用mutations中的方法使用`$store.dispatch`来调用actions中的方法使用`$store.getters`来访问getters中的计算属性。例如: ```javascript import store from './store.js' export default { computed: { count() { return this.$store.state.count } }, methods: { increment() { this.$store.commit('increment') }, asyncIncrement() { this.$store.dispatch('increment') }, getCount() { return this.$store.getters.getCount } } } ``` 4. 在Vue实例中引入Vuex store。在你的Vue实例中,通过在`store`选项中引入Vuex store使用它。例如: ```javascript import Vue from 'vue' import App from './App.vue' import store from './store.js' new Vue({ store, render: h => h(App) }).$mount('#app') ``` 这样,你就可以在你的Vue组件中使用Vuex来管理和共享状态了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Radom7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值