·vuex中有五个属性:state、getter、mutations、actions、modules
在这里面存:
在组件里的计算属性中使用:
state的使用
1、在state中存入需要获取的数据:
state: {
name:'张三',
age:'23',
sex:'男',
major:'物联网工程',
profession:'准程序员'
},
2、然后在需要使用的地方在computed中来获取:
在template中使用{{name}}、{{age}}即可使用。
computed:{
...mapState((['name','age']))
}
getters的使用
1、在getters中写好要使用的方法:
getters:{
phoneShow:state=>{
return state.phoneNum.substring(0,3)+'****'+state.phoneNum.substring(7,11)
},
sexShow:state=>{
return sex=>{
return state.desc[sex]
}
}
},
2、还是在computed中使用,和state类似:
computed:{
//...mapState((['name','age'])),
...mapGetters((['phoneShow'])),
sexShow(){
return this.$store.getters.sexShow(1)
}
}