我使用vuex下的axios从数据库中检索了我的数据
const state = {
giveaways: null
}
const actions = {
getGiveAways : ({commit}) =>{
axios({
url : '/prod/api/thresholds_settings',
method: 'post',
data : {
},
config: 'JOSN'
})
.then(response=>{
if(response.status == 200){
//console.log(response.data.total_giveaways);
commit('SET_GIVEAWAYS', response.data.total_giveaways)
}
})
.catch(error=>{
if(error.response){
console.log('something happened')
}
});
}
}
const mutations = {
SET_GIVEAWAYS : (state, obj)=>{
state.giveaways = obj
}
}
const getters = {
doneGiveAways(state){
return state.giveaways
}
}
在我的仪表板上。我有
import {mapState,mapGetters} from 'vuex'
export default{
data: () => ({
cards: [],
giveaways: ''
}),
computed:{
...mapState({
Giveaway: state => state.Threshold.giveaways
}),
doneGiveAways(){
return this.$store.getters.doneGiveAways
}
},
ready(){
//giveaways: this.Giveaways
//console.log(this.Giveaways);
},
created(){
const self = this
this.cards[0].result_val = 2
this.cards[2].result_val = 2;
},
mounted(){
this.$store.dispatch('getGiveAways');
console.log(this.giveaways);
}
}
我的问题是我需要将值从MAPSTATE Giveaway
传递给我的返回数据giveaways: ''
,因此当页面触发时,我可以使用this.giveaways
获得响应值。如果我只在我的html中调用{{giveaway}},则显示该值。但是我需要做类似this.giveaways = this.$store.state.Thresholds.giveaways
我将使用Stephan-V的建议并删除giveaways
的本地副本。但是我不知道您要宣布额外的giveaways
副本的特定原因,因此这是一个可以使用的解决方案:
在您的Dashboard.vue
中:
export default {
...
watch: {
Giveaway(value) {
this.giveaways = value
}
},
...
}
只需从数据对象删除giveaways
属性,然后将计算的doneGiveAways
重命名为giveaways
,就可以完成。
在这种情况下,不需要本地组件giveaway
数据属性。