Vuex encountered an unexpected mutation type within the module that was registered

I am trying to create a dynamic registered module in Vuex, but unfortunately it's not working as expected. Below is my store file:

import Vuex from 'vuex'
import descriptionModule from './module/descriptionModule';
const {state: stateModule, getters, mutations} = descriptionModule;

const createStore = () => {
  return new Vuex.Store({
    state: {
      descriptions: [],
    },
    mutations: {
      addDescriptions(state, payload){
        state.descriptions.push(state.descriptions.length + 1);
        createStore().registerModule(`descriptionModule${payload}`, {
          state: stateModule,
          getters,
          mutations,
          namespaced: true // making our module reusable
        });
      }
    }
  })
};

export default createStore

Here is the custom module that I will register:

const state = () => {
  return {description: ''}
};
const getters = {
  description: (state) => state.description
};
const mutations = {
  updateDescription(state, payloads){
    state.description = payloads;
  }
};
export default {
  state,getters,mutations
}

Next, these are the custom methods that will call the addDescriptions mutation and commit the updateDescription from the registeredModule:

beforeMount(){
  console.log("hahahaha");
  this.$store.commit('addDescriptions', this.id);
},
... more code ....
methods: {
      onType(editor, content){
        console.log(this.$store.state.a);
        console.log(this.$store.state);
        console.log(this.$store);
        this.$store.commit(`descriptionModule${this.id}/updateDescription`, content, {root: true})
      }
    }

Every time onType is called, I encounter an error saying "unknown mutation type: descriptionModuleeditor1/updateDescription" in the browser.

I have tried following this solution mentioned in this link, but it has not worked for me :(

If anyone can help solve this issue, I would greatly appreciate it. Apologies for any language errors in my explanation.

Answer №1

Implement the use of $store.registerModule() by component/pages within the beforeMount() lifecycle hook:

beforeMount(){
      this.$store.registerModule(`descriptionModule${this.id}`, {
        state: stateModule,
        getters,
        mutations,
        namespaced: true // ensuring reusability of our module
      });
    },

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

Using AngularJS's $watchCollection in combination with ng-repeat

Encountering difficulties receiving notifications for changes in a list when utilizing an input field within ng-repeat directly. However, I am able to modify values and receive notifications from $watchCollection. To illustrate: <!DOCTYPE html> < ...

Having difficulty invoking a JavaScript function at a global level

Currently, I am working on a nodejs application that involves mongoDB integration. In my code, I have created a function to extract specific data from MongoDB and save it in a variable called "docs". Despite multiple attempts to declare the function global ...

Unable to find custom components when using react-router

My goal is to improve the organization of my Routes in React and separate concerns. I am currently utilizing react-router-dom version 5. Within my Application Routes component, I have structured it with 3 children components: AuthenticatedRoutes PublicRo ...

Creating a like button using AJAX in Django

Is there a way to implement a like button in Django using ajax without the need for a page reload? I've tried setting up my HTML and Ajax function, but it's not working properly. <form method="POST" action="{% url 'video: ...

Generating a string indicating the range of days chosen

Imagine a scenario where there is a selection of days available to the user (they can choose multiple). The list includes Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, each with an associated number from 0 to 6. For instance, Sunday ...

What is the method for utilizing await within the NodeJs terminal?

When I use the Node Js terminal to perform quick tasks or analyze data, one limitation I face is the inability to wait for an asynchronous function: mymachine$ node > const request = require('request-promise-native') undefined > await requ ...

React application component fails to render due to conditional logic not being met

The implementation of this component seems correct to me. If the variable isReceivedSession is true, a <ReactFragment/> will be rendered; if it is false, a <Spinner/> should be displayed Inside the fragment, if either isLoadingApp or isLoading ...

Utilizing Angular's ng-model directive in combination with the required

I am attempting to implement a basic input field using html5 "required". The input field utilizes placeholder text, so I want the initial value of the input to be blank. I have connected the input to an ng-model that is set as empty initially to display th ...

Finding the scope of dynamically generated fields in AngularJS has proven to be quite challenging

I'm currently working on creating a dynamic form where users can add input fields by clicking a button. However, I am facing issues with fetching the value of the input field in the controller. Below is my form: <div ng-repeat="skill in skill_set" ...

Build a Google Map Widget within SurveyJs

Hey there, I'm new to working with SurveyJS and I'm trying to incorporate a Google Map widget into my SurveyJS. I followed some steps and successfully added the map in the Survey Designer section, but unfortunately, it's not loading in the T ...

Why is the image auto-swapping script failing to display images frequently?

I have a script that is currently running to rotate between two different logos on my webpage. What I am attempting to achieve is for the page to load and then seamlessly transition from one image to the other without any blank space. Below is the code I ...

The functionality of Javascript ceases to operate once I fill out the table

UPDATE: I managed to solve the issue on my own and will mark my answer as accepted in 24 hours according to the system's rules. I've been experimenting with Bootstrap using this repository: https://github.com/BlackrockDigital/startbootstrap-sb-a ...

Sending data to a button in a Vue.js component using Laravel

Currently, I am facing an issue while trying to pass the variable (id) of a specific comment within a v-for loop to a button that is rendered using blade syntax. I have successfully managed to post and fetch comments, but now I am struggling to delete spec ...

How to update a vuex state from a separate component?

In my current project, I have a modal component that relies on a state management store. This store keeps track of whether the modal is currently active or not. I would like to be able to trigger the opening of this modal from other components or even jus ...

Using HTML to execute a JavaScript function that transforms images

The script cutImageUp has been previously discussed on SE in a thread about Shattering image using canvas. However, I have encountered a different issue while attempting to use it. The HTML code I implemented seems to be ineffective, and despite my efforts ...

Selenium does not support running Javascript code

I am currently utilizing capybara in conjunction with Selenium as the driver. I am facing an issue where clicking on an element does not trigger the associated javascript to reveal a specific div. Below is my code snippet: scenario 'currently used t ...

Establish a callback function for opening a fresh window using JavaScript

Is there a simple method to assign a "callback" function to a new window opened in javascript? My goal is to execute a function from the parent window in the new window, but I want the parent to be able to specify the name of this specific function (withou ...

I'm curious about how to use nightwatch.js to target a dynamically generated element with a dynamic value

I'm facing an issue with identifying dynamically generated elements in a list box using JQuery or some other scripting language. My goal is to select a specific market value that is passed dynamically. The code I've currently implemented is as fo ...

Creating a selection area with CSS that appears transparent is a straightforward process

I'm currently exploring ways to implement a UI effect on a webpage that involves highlighting a specific area while covering the rest of the page with a semi-transparent black overlay, all using CSS only. What is the most common approach to achieving ...

Tips for developing a web-based whiteboard application

I am tasked with developing a web application that allows users to design factory layouts by dragging and dropping pre-built components like robots, manufacturing cells, and conveyors onto a canvas. The goal is to create something similar to websites such ...