Leveraging the power of mapState and mapMutations within a namespaced module

I'm having trouble accessing mutations and states after dividing my Vuex store into three modules. I've attempted various syntaxes, but none seem to be working for me.

MapStates: This is how I have set up the mapStates, with 'vendor' and 'root' as the module names.

...mapState({
  vendor: state => state.vendor.vendor,
  language: state => state.root.language
})

and using it like this:

console.log(this.vendor);

MapMutations: I believe I have correctly set up the mapMutations.

methods: {
  ...mapMutations('vendor', ['UPDATE_VENDOR', 'SET_VENDOR_APISTATE'])
}

and trying to access it like this:

this.$store.commit('UPDATE_VENDOR', response.data);

or

this.UPDATE_VENDOR(response.data);

None of these methods are working for me and I can't identify what mistake I might be making.

This is how my store is structured:

import vendorModule from './vendor/vendorModule';

const store = new Vuex.Store({
  modules: {
    customer: customerModule,
    root: rootModule,
    vendor: vendorModule
  }
});

with modules structured like this:

const vendorModule = {
  namespaced: true,
  state: () => ({
    vendor: null,
    vendorApiState: ENUM.INIT
  }),
  mutations: {
    UPDATE_VENDOR(state, vendor) {
      state.vendor = vendor;
      state.vendorApiState = ENUM.LOADED;
    }
  }
};

export default {
  vendorModule
};

EDIT I have realized that my modules were structured incorrectly, as Kelvin Omereshone pointed out, I used the incorrect syntax:

this.$store.commit('vendor/UPDATE_VENDOR', response.data);
.

The correct module structure is:

const state = () => ({
  vendor: null,
  vendorApiState: ENUM.INIT
});

const mutations = {
  UPDATE_VENDOR(state, vendor) {
    state.vendor = vendor;
    state.vendorApiState = ENUM.LOADED;
  }
};

export default {
  namespaced: true,
  state,
  mutations
};

Answer №1

To incorporate

this.$store.commit('UPDATE_VENDOR', response.data);
, you don't need to use mapMutations. Just implement it in this way:

this.$store.commit('vendor/UPDATE_VENDOR', response.data);

Keep in mind that the module name should precede the Mutation name, separated by a forward slash /

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to interacting with the Li element using JavaScript in Selenium

Is there a way to click on the item inside the li element using a Selenium script with JavaScript? I've tried different methods like By.cssSelector or by css, but I keep getting an ElementClickInterceptedError: element click intercepted:Other element ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

Exploring elements with Watir WebDriver and ExtJS

Currently, I am performing an acceptance test using watir-webdriver with Ruby. I have a question regarding ExtJs support with Watir webdriver. Is it possible to locate elements that are dynamically generated by ExtJS? I have attempted the following: @brow ...

Switch up the AJAX jQuery URL based on checkboxes selected

As I work with four checkboxes and fetch JSON data from an API using jQuery AJAX, my goal is to dynamically change the URL based on checkbox selection. Although the ajax code functions properly within the click function, it fails when placed outside of it. ...

Every time I click, the click event is getting attached repeatedly through the on method

Here is the HTML code that I am working with: <div class="connect"> <ul> <li><a href="#">Assign a Task</a></li> <li><a attr="viewCard" href="#">View Ca ...

What are the distinct roles of the server and client in a Nuxt SSR application?

Currently, I am working with Nuxt 2.13 and developing an e-commerce platform. However, I have encountered some server resource issues where the initial site load is taking longer than expected (although route changes are fast and smooth). I am curious to ...

Guide to removing a colon from my JSON.stringify output

I'm encountering an issue with a JSON.stringify function. It is adding a colon after the JSON object. axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan', JSON.stringify({ name: currentProduct.product. ...

The exceptional speed of jQuery's each method sets it apart

I'm currently facing an issue where I am unable to successfully add the attribute data-size to my parent div. My code snippet is as follows: var width, height; $(".galerie img").each(function() { width = this.naturalWidth; height = this.naturalH ...

Is there a way to restrict the date-picker in vue to only allow Mondays?

Is there a way to customize the v-data-picker component in my Vue project so that only Mondays are allowed to display, with all other days being disabled? Here is the code I currently have: <template> <v-row justify="center"> & ...

Enforce directory organization and file naming conventions within a git repository by leveraging eslint

How can I enforce a specific naming structure for folders and subfolders? I not only want to control the styling of the names (kebab, camel), but also the actual names of the folders and files themselves. For example, consider the following paths: ./src/ ...

The update feature seems to be malfunctioning within the MEAN Stack environment, specifically with node.js and angular js

Looking for some assistance as a beginner in the mean stack. I'm trying to update a record using the update function, but it's not working as expected. I need to update a specific object based on its ID, however, I'm encountering issues wit ...

Getting variables from Mounted() in VueJS Methods

As a newcomer to Vue, I need help figuring out how to access and utilize variables created within the Mounted() lifecycle hook in my methods. Here is the code snippet: Template <select class="controls" @change="getCatval()"> Sc ...

Tips for concealing labels until the submit button is activated

I am currently handling a project involving a Spring Controller and JSP. Within a JSP page, I have included a button labeled Process. Upon clicking this button, two radio buttons are displayed below it along with a Submit button. After tapping the Submit ...

Parsing and Displaying JSON Data from a Python DataFrame in D3

Trying to create a stock chart, I encountered an issue with parsing the json file output by my python dataframe. The example code from http://bl.ocks.org/mbostock/3884955 does not seem to fit the format of my data: The json looks like this: var dataset = ...

Sending Django Variable With Javascript

As a newcomer to Javascript, I am grappling with retrieving the value of a variable from my HTML form. My current code seems to be somewhat functional - I added an alert to test the logic and found that the else statement is working fine. However, I'm ...

Generating a JSON tree hierarchy from an array of nested objects

I have a JSON array of objects that looks like this: [{ "vehicleid": 3, "name": "Teste2VDD", "brand": "Scania", "model": "6x2", "class": "class1", "region": "Curitiba", "customer": "Cliente A", "customerid": 1 }, { "veh ...

Revamp List Model through Ajax Integration in ASP .NET MVC5

Could someone please provide a hint on how to update the Model list in the view page after calling the Action Result with an Ajax request? Specifically, how can I refresh the current list model with the result of the Ajax call back? Here is the code for m ...

Retrieve the child element that is being clicked

Alright, I'm facing a little issue here (it seems simple, but I just can't seem to crack it)... Let me paint the picture with a snippet of HTML code below: <!-- New Website #1 --> <!DOCTYPE html> <html style='min-height:0px; ...

Initiate an AJAX call and in the event that a particular object is found in the JSON response, proceed to send a subsequent request targeting

My objective is to make an AJAX request to a URL and expect a JSON response consisting of two objects: group_id and next_page. If the next_page object is set, I want to send another AJAX request using the value of next_page as the URL. If there is no next_ ...

Tips to center a circular progress bar in Material UI

Does anyone know how to properly center-align a circular progress bar in a login form using material UI? I'm having trouble with it not being positioned correctly: screenshot {isLoading && <CircularProgress />} {user?.ema ...