如果编码过程中我们需要对state中的数据进行加工后返回,mutations是用来更改state的,那么我们就用到了getters,代码如下:
store->index
export default {
state:{
curInx:0,
name: '小王',
age: '18'
},
mutations:{
// setCurInx(state,index){
// console.log(index)
// state.curInx = index
// }
setCurInx(state,payload){
let {index,text} = payload;
console.log(index,text)
state.curInx = index
}
},
getters:{
getMyInfo(state){
return `我的名字是${state.name},我今年${state.age}岁`;
}
}
}
Part1->index.vue
<template>
<div>{{myInfo}}</div>
</template>
<script>
export default{
name: 'MyPart',
data(){
return{
myInfo: ''
}
},
mounted(){
this.myInfo = this.$store.getters.getMyInfo
}
}
</script>
<style scoped>
</style>