Search code examples
javascriptvue.jsfrontendvuexvue-router

Vuex problems, important project, MapActions,MapGetters


The reason why I am writing here because I just got a job as a front-end developer and on the first day they threw at me a big project and I lost, very lost, hopeless.

My job would be that I am having a home page where there is a table with some information for example date, hours, the daily wind speed, daily celsius, and under the table there would be information which can be generated from a config panel. So basically with Vue-router am having two pages, one with the home and one with the config panel, now in the config panel I am having a form where you can write your text you want to display to the home page and my problem is that I have no clue how to display that text from one router to another I tried to make a method where on submitting the form it should somehow commit that message to my Vuex state but when I am about to write it out to the home page it is now showing,

This is what I have in my modules (vuex file) :

const state = {
    message: '',

};
const getters = {
    message: (state) => {
        return state.message;
    },

};

const actions = {
    setMessage: ({
        commit,
        state
    }, newValue) => {
        commit('SET_MESSAGE', newValue);
        return state.message;
    },

};
const mutations = {
    SET_MESSAGE: (state, newValue) => {
        state.message = newValue;
        console.log(state.message);
    },

};

export default {
    state,
    getters,
    actions,
    mutations,
};

when I am console logging the state.message the text which got submitted is there now I need to put this text to my homepage, please help me with that I know that this should be an easy one but I cannot solve it.


Solution

  • On home page you should add

    created() {
        this.$store.dispatch('SET_MESSAGE');
    }
    

    This code will call SET_MESSAGE

    computed: {
        ...mapGetters({
            message: 'message',
        }),
    }
    

    After adding this part you can take message value simple call "this.message" in methods or "message" in template