vuex的getters处理数据

Posted on   猫头唔食鱼  阅读(3477)  评论(0编辑  收藏  举报

getters是用来处理state里的数据的

getters传递一个值state

例子:

store.js

复制代码
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const store = new Vuex.Store({
    state:{
        prod:[
            {name:"zs",age:12},
            {name:"ls",age:13},
            {name:"ww",age:14},
        ]
    },
    getters:{
        getValue(state){
          var item =   state.prod.map((ele,index)=>{
                return {
                    name:ele.name+"__技术部",
                    age:ele.age+10
                }
            })
            return item ;
        }
    }
})
复制代码

 

Home.vue

复制代码
<template>
<div>
    <table>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <tr v-for="(item,i) in getValue">
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
        </tr>
    </table>
    <hr>
    <table>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        <tr v-for="(item,i) in getItem">
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
        </tr>
    </table>
</div>
</template>
<script>
export default {
  name: "Home",
  data () {
    return {
    };
  },
  computed:{
      getValue(){
         return  this.$store.state.prod;
      },
      getItem(){
          return this.$store.getters.getValue
      }
  }
}
</script>
<style lang="css" scoped>
</style>
复制代码

结果:

 

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示