Vuex: Error - Namespaced action "is not a function"

After importing a Vuex module, everything is working smoothly without any issues. However, as soon as I try to namespace it, an error message pops up saying that the action is "not a function" when it is called.

This is how it looks like in index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import MainWindow from './modules/MainWindow';
import ArtEditor from './modules/ArtEditor';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
    },
    mutations: {
    },
    actions: {
    },
    modules: {
        MainWindow,
        ArtEditor
    }
});

The ArtEditor module code:

const state = {
    selectedColor : "#FFFFFF"
}

const getters = {
    selectedColor: state => state.selectedColor
};

const actions = {
    selectColor({commit}, newColor){
        commit('selectColor', newColor);
        console.log(newColor)
    }
};

const mutations = {
    selectColor: (state, newColor) => state.selectedColor = newColor
};

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

In a Vue component:

import {mapActions, mapGetters} from 'vuex';
...
methods:{
    ...mapActions(['ArtEditor/selectColor']),
    colorChanged(color){
        this.selectColor(color);
    }
},
...

Whenever the color changes, an error occurs with the message

this.selectColor is not a function
. The confusion arises as most of the documentation on namespaced modules either does not import external Vuex modules or uses dispatch() instead of mapActions(). There seems to be something amiss that I cannot figure out.

Answer №1

You are not using the correct syntax, you should do one of the following options:

...mapActions(['ArtEditor/selectColor']),
colorChanged(color){
    this['ArtEditor/selectColor'](color);
}

Alternatively, you can separate the action path while mapping like this:

...mapActions('ArtEditor', ['selectColor']),
colorChanged(color){
    this.selectColor(color);
}

For more information, please refer to binding helpers with namespace.

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

AngularJS UI Select with StartsWith Searching Feature

I'm currently working on an AngularJS application where I have implemented UI Select. Below is the HTML code snippet: <ui-select name="service" multiple ng-model="service.ServiceName" close-on-select="false" > <ui-select-matc ...

Guide to Increasing Table Index within Nested Loop in Vue.js 3

How can I increment table indices within a nested loop in Vue.js 3? I am currently working with two-level deep arrays using Vue.js, and I need an index that starts at 1 and increases for each item in the top-level array. The desired output is as follows: ...

What is the best way to rotate points around a mesh?

I am attempting to utilize Three.js to create a collection of points using Three.Points. My goal is to have these points rotate around a single point or mesh. I have already successfully generated the points randomly within a cylinder region, following the ...

What is the best way to align a label, input box, and a small search image on the same line

I'm currently working on developing a mobile page using jQuery Mobile version 1.4.4. I've managed to get the label and input box to display correctly in one line, but I'm encountering an issue with the small search image appearing on the nex ...

Utilize Next.js to send an image to an email by leveraging the renderToString function on the API routes

I need help with sending styled emails that contain images. Currently, I am utilizing the renderToString method to pass props into my component. So far, everything is functioning correctly in the API routes. mport client from "@/lib/prisma"; im ...

How can I add an image to a canvas using a button?

Having trouble with my studies and looking to create a custom shirt website. Posted below is the beginner code I have: If anyone knows how to upload an image onto a shirt canvas using a button, as well as change the shirt color with another button, please ...

Passing information between VueJS3 routesStepThrough

Exploring the capabilities of Vue3. I'm interested in defining roles allowed in the route setup, and then utilizing that value in the beforeRouteEnter guard. Here's what I have in mind: { path: "secure/page", name: "SecureP ...

A guide on setting up ExpressJS Error Handling to display custom error messages based on the corresponding HTTP Status codes

I am struggling to figure out how to implement this task. You need to create an error on the /error path and pass it to the next function. Make sure to use appropriate error status codes for displaying different types of error messages. I attempted using ...

Incorporate a versatile navigation menu into your HTML pages

At the moment, my website is built with static hand-coded HTML pages. I manually add a navigation menu to each page individually. However, I am looking for a more efficient way to manage this process by maintaining a single user resource file that contains ...

What is the most effective approach for addressing errors in both the server and client sides while utilizing nodejs and express?

Seeking the most effective approach for handling errors in a response - request scenario. Here is an example of a route that receives a request: app.get('/getInfo', function (req, res, next) { let obj = {} try { obj = { ...

Using socket.io to filter MongoDB documents by their unique object IDs

I am facing an issue where I need to retrieve a specific document from a MongoDB collection using the object ID when working with Socket.io. Since `findById()` is not effective in this case, and I am unable to use `find({ "_id" : _id})`, I am unsure about ...

jquery's animation causing elements to move abruptly when containing text

For the past hour, I've been grappling with jQuery and could really use some assistance. The following example showcases smooth animations: when you click on either of the divs, the selected div will move left or right if the viewport is wider than 70 ...

Adjusting the width of the xAxis in Vuejs with Highcharts

I'm working on creating a bar graph with HighchartJS. Everything works perfectly when the start date is set to 1st Jan. However, when I dynamically change the data to start from 2nd Jan, the grouped Bar chart doesn't display properly. It seems th ...

Webpack encountering issues while trying to compile the Underscores Theme

As I work on developing two websites using the _s Theme by Automattic, I have encountered an issue. While they run smoothly locally with npm run dev, I face a problem when attempting to deploy them by running npm run build. The build process fails every ti ...

Changing buffer from base64 to UTF-8 encoding in Node.js

My application imports messages from the Notes folder of Gmail using the imap npm module. When following the example on their GitHub page, all message contents are read into a buffer: stream.on('data', function(chunk) { count += chunk.len ...

The npm encountered an error stating that it was unable to locate the module '@vue/cli-plugin-babel/preset'

Currently, I am diving into a video tutorial to learn the ins and outs of Vue and how to set up components. Everything was smooth sailing yesterday as I ran npm run serve with no problems. However, when I returned to continue the tutorial today and tried ...

Troubleshooting Problem with ListItem Alignment in Material UI v0 involving Custom Avatar Component

Material UI version: v0.20.0 I'm facing an issue with aligning the leftAvatar value using the CustomAvatar component, as shown in the attached screenshot. Any assistance would be appreciated. CustomAvatar: The functionality of this component is cond ...

Receiving communication without the need for port forwarding

My goal is to establish a system where a server can send messages to clients at any given time, ensuring that when a message is sent, it is received almost immediately (ideally within 1 second or less). I am looking for a way to achieve this without having ...

Generating a safe POST connection with express.js

Is there a simple method to generate a link for submitting a POST request using Express.js or a plugin? This approach can also be employed to enhance security for important actions like user deletion, including CSRF protection. In some PHP frameworks lik ...

Tips for shifting the canvas to the back using JavaScript

I am pondering how to make the box move backward once it reaches 1000px (canvas.width), but I am unsure how to proceed after the If condition. Here is my script, can anyone help me out? var canvas = document.querySelector("canvas"); var canvasCT = canvas ...