F
F
fierfoxik2017-08-29 13:10:32
Vue.js
fierfoxik, 2017-08-29 13:10:32

Why is action not sent to mutation?

Good time.
Moved from react/redux to vue/vuex and had issues with vuex.
For some reason, when dispatch or cmmit from action, not one of the methods does not come there, but only the transferred data comes.
Also, when dispatching an action from a component, we receive data, but the data of the court does not go anywhere from the action, because the received data in the mutation is empty.
store

import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getters from './getters'
import * as mutations from './mutations'

Vue.use(Vuex)

const initialState = {
  settings: JSON.parse(localStorage.getItem('widgetSettings')) || []
};

const inBrowser = typeof window !== 'undefined'

// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) || initialState


export const store = new Vuex.Store({
  state,
  actions,
  mutations,
  getters
})

actions
import {SAVE_WIDGET_SETTINGS} from '../constants'

export const saveSettings = ( data) => {
        console.log(data)
        return {SAVE_WIDGET_SETTINGS, data}
};

mutations
import {SAVE_WIDGET_SETTINGS} from '../constants/index'


export const mutations = {
    SAVE_WIDGET_SETTINGS (state, data) {  state.settings.push(data)}

}

component
<div class="widgetsblock_selectors_grid_col">
  <div class="widgetsblock_selectors_selector">
    <button class="btn btn-secondary btn-sm" v-on:click="submit" type="button">Сохранить</button>
  </div>
</div>
<script>
    import { mapActions } from 'vuex'
    import { mapState } from 'vuex'
    import { saveSettings } from '../../vuex/actions'


    export default {
        name: 'WidgetsOptions',
        data () {
            return {
                checkedNames: []
            }
        },
        mounted: function () {
            this.loadSettings()
        },
        methods: {
            submit: function() {
                localStorage.setItem('widgetSettings', JSON.stringify(this.checkedNames));

                mapActions([(saveSettings(this.checkedNames))]);
            }
        }
    }
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lavezzi1, 2017-08-29
@lavezzi1

Action should be like this:

saveSettings({ commit }, data) => {
        console.log(data)
        commit(SAVE_WIDGET_SETTINGS, data)
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question