Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vuex如何在数据属性中传递状态/getter_Javascript_Vue.js_Vuex - Fatal编程技术网

Javascript Vuex如何在数据属性中传递状态/getter

Javascript Vuex如何在数据属性中传递状态/getter,javascript,vue.js,vuex,Javascript,Vue.js,Vuex,我在vuex下使用axios从数据库检索数据 const state = { giveaways: null } const actions = { getGiveAways : ({commit}) =>{ axios({ url : '/prod/api/thresholds_settings', method: 'post', data : { },

我在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
    }
}
在我的Dashboard.vue中,我有

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
的东西,只需从数据对象中删除
giveaways
属性,并将计算的
doneGiveAways
重命名为
giveaways
,就完成了


在这种情况下,不需要本地组件
赠品
数据属性。

我将使用Stephan-v的建议并删除
赠品的本地副本
。但是我不知道您声明额外的
赠品副本的具体原因是什么,所以这里有一个可行的解决方案:

仪表板.vue中添加:

export default {
    ...
    watch: {
        Giveaway(value) {
            this.giveaways = value
        }
    },
    ...
}