首页 > 其他分享 >vuex中的getters

vuex中的getters

时间:2022-11-16 09:22:17浏览次数:52  
标签:function vaule getters state context vuex store

当state中的数据需要经过加工后在使用

//该文件用于创建vuex中的store

//引入vuex
import Vue from 'vue'
import Vuex from 'vuex'
//使用插件
Vue.use(Vuex);
//猪备actions--用于响应组件中的动作
const actions={
    // jia:function (context,vaule) {
    //     context.commit("JIA", vaule)
    // },
    // jian: function (context, vaule) {
    //     context.commit("JIAN", vaule)
    // },
    jiaOdd: function (context, vaule) {

        if (context.state.sum%2) {
            context.commit("JIA", vaule)
        }   
    },
    jiaWate: function (context, vaule) {
        setTimeout(() => {
            context.commit("JIA", vaule)
        }, 500);
    },

}

// 准备mutations--用于操作数据(state)
const mutations = {
    JIA:function (state,value) {
        state.sum+=value;
    },
    JIAN: function (state, value) {
        state.sum -= value;
    },
}

//准备state--用于存储数据
const state = {
    sum: 0//当前的和
}

//用于将state中的数据进行加工
const  getters={
    bigSum(state){
        return state.sum*10
    }
}
//创建store
const store=new Vuex.Store({
    actions: actions,
    mutations: mutations,
    state: state,
    getters: getters
});



//导出store

export default store
<template>
  <div class="count">
    <h2 > 当前求和为 :{{$store.state.sum}}</h2>
    <h3 > 当前求和放大十倍为为 :{{$store.getters.bigSum}}</h3>
    <select v-model.number="n">
      <option :value="1">1 </option>
      <option :value="2">2 </option>
      <option :value="3">3 </option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前求和为奇数再加</button>
    <button @click="incrementWate">等一等再加</button>
  </div>
</template>

<script>
import {nanoid} from 'nanoid'

export default {
    name:"Count",
    data() {
      return {
        n:1,//用户选择的数字
        
      }
    },
    mounted() {
      console.log(this.$store)
    },
    methods: {
      increment(){
        // this.$store.dispatch('jia',this.n)
        this.$store.commit('JIA',this.n)
      },
      decrement(){
        this.$store.commit('JIAN',this.n)
      },
      incrementOdd(){
       
        // if (this.Sstore.state.sum%2) {
           this.$store.dispatch('jiaOdd',this.n)
        // }
      },
      incrementWate(){
        // setTimeout(() => {
         this.$store.dispatch('jiaWate',this.n)
        // }, 500);
      },
    },
    
}
</script>

<style scoped>
   button{
     margin: 5px;
   }
</style>

这时候使用配置项getters

 

标签:function,vaule,getters,state,context,vuex,store
From: https://www.cnblogs.com/xiaobaizitaibai/p/16894769.html

相关文章

  • VUEX的基本使用之加减案例
    组件count<template><divclass="count"><h2>当前求和为:{{$store.state.sum}}</h2><selectv-model.number="n"><option:value="1">1</option......
  • Vuex 数据持久化(vuex-persistedstate)
    使用conststore=newVuex.Store({modules:{user:{},},getters,actions,//异步mutations,//同步plugins:[createPersistedState({......
  • Vuex 持久化数据更新(vuex-persistedstate)
    在Vuex的使用过程中,会面临数据持久化问题,如:用户数据、菜单数据、必要的信息数据等。遇到问题:改变数据后F5刷新页面,数据不改变使用方式exportdefault{mounted......
  • Vue2.x下的各组件如何传递信息?(不使用Vuex)
    Vue2(选项式)一,父组件向子组件传递数据:介绍:在引用的子组件中定义自定义属性msg与user.可以通过v-bind绑定要发送的数据。  在子组件中使用props接收自定义属性msg......
  • Vuex管理dialog、toast等常见全局性组件状态时唯一性的问题
    前言工作中经常会用到类似于dialog、toast、popover等一些状态提示组件。对于这种全局性的组件,通常会用到vuex来管理组件的信息。这样的好处是代码维护起来更加友好,但......
  • Vue面试题48:你觉得Vuex有什么缺点?(总结自B站up主‘前端杨村长’视频,仅供自用学习)
    体验使用模块:用起来比较繁琐,使用模式也不统一,基本上得不到类型系统的任何支持:conststore=createStore({ modules:{a:moduleA}})store.state.a......
  • 46.使用过vuex和vue-router吗
    使用过,vuex是状态管理工具,它的数据可以被所有的组件获取,方法可以被所有的组件调用;vuex 的内部的运行机制:state提供了数据驱动视图,dispath派发actions执行异步操作,comm......
  • 组件之间的通信2-->vuex状态管理
    在上期,我们讲了父子组件的传递方式,但是,如果我们想知道这些数据从哪里来的话,就需要一层一层找父组件,最后才能找到数据,容易造成Prop逐级透传问题今天,我们将介绍另一......
  • 谈一谈 vuex 的运行机制
    Vuex提供数据(state)来驱动视图(vuecomponents),通过dispath派发actions,在其中可以做一些异步的操作,然后通过commit来提交mutations,最后mutations来更改state。 ......
  • 快速上手VueX
    前言本文主要介绍了VueX状态管理器、VueX的核心概念、store.js以及vuex的工作流程。理解了这些概念可以让我们快速上手使用VueX,实现对Vue的状态管理。VueXVueX是对Vue的......