Unable to send a namespaced action from a different module: [vuex] action type is not recognized

In my coding project, I am working with two different modules called activities and alerts. One of the requirements is that whenever an activity is added, I need to trigger an alert action with the namespaced identifier alerts/SHOW.

This functionality was successfully implemented when I directly called the action from a component, utilizing the createNamespacedHelpers method provided by Vuex with the namespace set to alerts.

However, I encountered an issue when I tried dispatching the action from another module within a different namespace. The error message thrown was:

[vuex] unknown action type: SHOW

I am unsure about what could be causing this error.

To provide some context, I am invoking the ADD action using another instance of createNamespacedHelpers, this time for the activities namespace. Additionally, I have utilized the { root: true } option as specified in the Vuex module documentation.

AddActivityButton.vue

<template>
  <button @click="addActivity(activity)"
          type="button"
          :disabled="activityCount >= maxActivities"
          class="btn btn-secondary">{{ activity.name }}
  </button>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
import { ADD } from "@/store/modules/activities";

const { mapActions, mapGetters, mapState } = createNamespacedHelpers(
  "activities"
);

export default {
  methods: {
    ...mapActions({
      addActivity: ADD
    })
  },
  computed: {
    ...mapState(["maxActivities"]),
    ...mapGetters(["activityCount"])
  },
  props: {
    activity: {
      type: Object,
      required: true
    }
  }
};
</script>

activities.js

import uuid from "uuid/v4";
import { SHOW as SHOW_ALERT } from "@/store/modules/alerts";

export const ADD = "ADD";
export const RESET = "RESET";
export const MAX_ACTIVITIES = 15;

const state = {
  items: [
    { id: 1, name: "A" },
    { id: 2, name: "B" },
    { id: 3, name: "C" },
    { id: 4, name: "D" },
    { id: 5, name: "E" },
    { id: 6, name: "F" }
  ],
  activities: [],
  maxActivities: MAX_ACTIVITIES
};

const getters = {
  activityCount(state) {
    return state.activities.length;
  }
};

const mutations = {
  [ADD](state, activity) {
    state.activities = [...state.activities, { ...activity, id: uuid() }];
  },
  [RESET](state) {
    state.activities = [];
  }
};

const actions = {
  [ADD]({ dispatch, commit, getters }, activity) {
    if (getters.activityCount >= MAX_ACTIVITIES) {
      return null;
    }
    if (getters.activityCount > 1) {
      dispatch(SHOW_ALERT, null, { root: true }); // This is the problematic line.
    }
    commit(ADD, activity);
  }
};

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

alerts.js

export const SHOW = "SHOW";

const state = {
  show: false
};

const mutations = {
  [SHOW](state) {
    state.show = true;
  }
};

const actions = {
  [SHOW]({ commit }) {
    commit(SHOW);
  }
};

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

store.js

import Vue from "vue";
import Vuex from "vuex";
import activities from "./modules/activities";
import alerts from "./modules/alerts";

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    activities,
    alerts
  }
});

Answer №1

It's important to specify the namespace when dispatching actions:

dispatch('notifications/' + SHOW_NOTIFICATION, null, { root: true });

An alternative way is to use template literals like this:

dispatch(`notifications/${SHOW_NOTIFICATION}`, null, { root: true });

Answer №2

When you need to trigger an action from a module store within a component

this.$store.dispatch("alerts/SHOW", data);

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

How to simulate keyboard events when a dropdown list is opened in an Angular application

Requirement- A situation arises where upon opening the dropdown menu, pressing the delete key on the keyboard should reset the index to -1. Steps to reproduce the issue: 1. Click on the dropdown and select an option from the menu. 2. Click on the dropdow ...

What is the best way to arrange the keys within a nested object in JavaScript?

Question: { "foo": "bar", "bar": "baz", "baz" : { "nestedKey": "foo" } } In order to sign this request using the Hmac512 algorithm, I must first stringify the object. I am concerned that if the key order is not preserved, the generated signature on the ...

The console experienced a forced reflow when a tooltip appeared on the slider handle while executing JavaScript

I am currently facing an issue with my web page that includes various elements along with the Ant.design slider. The values of the slider are being controlled through React states. However, I have noticed that when the slider tooltip is enabled, the respon ...

Google-play-scraper encounters an unhandled promise rejection

I'm trying to use the google-play-scraper library with Node.js, but I keep encountering an error when passing a variable as the 'appId'. How can I resolve this issue? Example that works: var gplay = require('google-play-scraper') ...

I prefer to avoid using the "#" sign in URLs

<a href="#" onClick="load_page()">intro</a> I am trying to avoid displaying the # sign in the URL, and I would like it to appear like this instead: www.mydomain.com/ However, it currently displays as follows: www.mydomain.com/# Is there a ...

Issue with ng-true-value in AngularJS version 1.6.1 - not functioning as expected

Recently, I delved into AngularJS and followed an online tutorial that showcased how to utilize ng-true-value and ng-false-value. Here's the snippet: <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis ...

The mysterious case of the vanishing close button on Curator.io's mobile

Currently, I am using curator.io aggregator to showcase my Instagram feed on the website. An issue arises when switching to mobile view as the close button (class="crt-close") disappears. You can see an example of this here: To replicate the pr ...

Issues with HTML structure across various devices

Being a novice in web development, I've been experimenting with creating a modal body. https://i.sstatic.net/Qk5BR.png The code snippet below represents my attempt at structuring the modal body: <div className="row modalRowMargin textStyle" > ...

Is there a way to develop a login form that retrieves information from a Google Sheet database?

Please help me with a solution. I have developed a registration form which effectively saves users' information in a Google Sheet. However, now I am yearning to create a login form that verifies the stored data and redirects the user to a different pa ...

Adjust the position of a textarea on an image using a slider

Currently, I am exploring how to creatively position a textarea on an image. The idea is to overlay the text on the image within the textarea and then use a slider to adjust the vertical position of the text as a percentage of the image height. For instanc ...

Evaluate the script based on the number of words, not the number of characters

I have encountered an issue where I am only able to count the characters of a content, but what I really need is the word length. Specifically, I want the CSS to take action when there are words of 20 characters, but not if those 20 characters consist of m ...

The Typescript decorator is unable to access the property type within its own scope

I am currently in the process of developing a dependency injector for use in my VUE js project. Recently, I created an Inject decorator with the intention of accessing a property type. It was functioning perfectly fine yesterday, but now it seems that som ...

How can JavaScript be used to rearrange the placement of DOM elements?

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div class="boxes"> <div class="red" style="width: 300px; height: 300px; color: red"></div> <div class="blue ...

AngularJS Vimeo API Request Error: "401 Authorization Required"

I've been experimenting with making external API calls to Vimeo from my AngularJS code using $http.jsonp. However, I keep receiving a 401 Authorization required error even though I have included my authorization key in the header. I encountered a simi ...

Leveraging weather application programming interfaces

I am trying to set up a basic webpage that can display tide information from wunderground.com. However, for some reason I am not seeing any results on the page. I have included the load function in hopes of at least getting something to appear when the p ...

Discover the world of Google Chrome Apps with the power of chrome.storage.local

While building an application, I encountered a perplexing issue that I need help with: The task involves reading a JSON file and storing its content in localStorage using chrome.storage.local in a Chrome app. Here is a snippet from the JSON file: { "s ...

PHP code to display "ed elements that do not adjust height"

A php script is being utilized to scan a directory and populate a gallery with all the images. Within the <div id="content"> tag, the function <?php create_gallery("path"); ?> loads all the images. The issue arises when the height of the <d ...

Modify the key within an array of objects that share a common key

I have an object structured as follows: NewObjName: Object { OLDCOLUMNNAME1: "NEWCOLUMN_NAME1", OLDCOLUMNNAME2: "NEWCOLUMN_NAME2", OLDCOLUMNNAME3: "NEWCOLUMN_NAME3"} Next, there is an array containing objects in this format: ...

Preventing the addition of hash tags to the URL by the anything slider

Why does the Anything Slider add hash tags like #&panel1-1 to the end of the URL? I attempted using hashtags:false but it was unsuccessful. Are there alternative methods to prevent it from creating these hashtags? ...

What causes the error "property does not exist on type" when using object destructuring?

Why am I encountering an error in TypeScript when using Object destructuring? The JavaScript code executes without any issues, but TypeScript is showing errors. fn error: This expression is not callable. Not all elements of type '(() => void) | ...