Classbased Typescript implementation for managing state with a Vuex store

Hey everyone, I'm currently working on a Vue project with Vuex using decorators for strong typing in my template.

As someone new to the concept of stores, I am struggling to understand how to properly configure my store to work as expected in my components.

My end goal is to be able to call the actions and getters of my different store modules like this:

this.$store.nameOfMyModule.NameOfAction
this.$store.<nameOfMyModule.NameOfGetter

An example specific to my project would be:

this.$store.tickets.tickets
this.$store.tickets.fetchTickets

Here is the link to the repository.

Answer №1

Consider utilizing mapActions and mapGetters for a more organized approach:

import { mapActions, mapGetters } from 'vuex'

//...

computed: {
   ...mapGetters('tickets', [
        tickets
   ]),
   ...mapGetters('users', [
        users
   ])
},

methods : {
   ...mapActions('tickets', [
        fetchTickets
   ]),
   ...mapActions('users', [
        fetchUsers
   ])
}
//...

Following this, you can easily access this.fetchTickets() and this.tickets in your component.

Remember to assign fetchUsers to the action's users module instead of fetchTickets.

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

Exploring the concept of 'Abstract classes' within the Svelte framework

As someone who is relatively new to Svelte and frontend development (with primary experience in Rust/C++/Python), I hope you can forgive me for asking what might seem like a basic question. My goal is to showcase different kinds of time-indexed data, with ...

Unable to compile TypeScript files using gulp while targeting ES5

After starting my first Angular2 app, I encountered an issue where I couldn't use gulp to compile for es5. Below is the dependencies file: "dependencies": { "@angular/common": "2.0.0", "@angular/compiler": "2.0.0", "@angular/compiler-cli ...

Tips for maintaining an open ng-multiselect-dropdown at all times

https://www.npmjs.com/package/ng-multiselect-dropdown I have integrated the ng multiselect dropdown in my Angular project and I am facing an issue where I want to keep it always open. I attempted using the defaultOpen option but it closes automatically. ...

The Vue.js auto-suggest input feature is causing the HTML layout to have excess whitespace

There seems to be an issue with the auto suggest feature causing extra spaces in the HTML, which in turn pushes down elements like H1, H2, and paragraphs when typing in the suggestion box. App.vue <template> <div id="app"> <Autoc ...

Having difficulty understanding Symbol.iterator and the return value type in a for-of loop while using TypeScript

Currently, I am delving into type script and embarking on a journey to learn how to craft generic containers for educational purposes. I have developed a LinkedList class with the intention of incorporating the ability to iterate over it, like so: for (co ...

Is it secure to transmit Tenant ID's as GET parameters to the API in a Multi-Tenant database environment?

When working with a Multi-Tenant database, is it secure to pass Tenant ID's as query string parameters to the API while using popular JavaScript-based UI platforms like Angular or Vue and ensuring both the site and the API are HTTPS? For example, let ...

Why does Vue reset a v-binded property when a v-model property changes?

Apologies if the title doesn't fully capture my query. In the brief Vue code snippet below, I have 2 input fields. One utilizes v-bind and the other uses v-model. I am puzzled as to why changing the second input resets the value of the first input. ...

Issue with Vue3: The imported module is not defined

Update: I recently downgraded vue-cli to version 4.5.19 and now the code is functioning properly. It seems like there might be an issue related to vue-cli or its dependencies. I encountered a problem while working on a simple vue.js project using vue-cli. ...

Tips for implementing a selection event in an auto complete dropdown menu:

I am struggling with detecting the selection in this Combobox. For example, if I choose "Massachusetts", I want to be able to access that information. @change call API + populated list - Done ✅ @select doSomething() - Not Done ❌ I have a hard time d ...

Display the React component following a redirect in a Next.js application that utilizes server-side rendering

Just starting out with next.js and encountering a problem that I can't seem to solve. I have some static links that are redirecting to search.tsx under the pages folder. Current behavior: When clicking on any of the links, it waits for the API respo ...

Ways to perform testing on SetTimeout function invoked in VUE Created HOOK using Vue utilities and JEST

I am currently working on an alert component that has a boolean flag called isVisible. This flag is initially set to false, but becomes true when the component is created. In addition, in the created hook of the component, there is a setTimeout function th ...

Creating an array of objects data is a breeze with Vue.js

I have an array of data containing selected items, and I need to extract the IDs from this array into a new array so that I can send only the IDs to the back-end. Sample Code method toggleSelection(rows) { console.log('this.multipleSelection : &a ...

Problem with Ionic 2 checkboxes in segment controls

I encountered an issue with my screen layout. The problem arises when I select checkboxes from the first segment (Man Segment) and move to the second segment (Woman Segment) to choose other checkboxes. Upon returning to the first segment, all my previous ...

I am experiencing an issue with the PUT method on my API as it is not correctly setting the req.body data

Below is the code snippet for implementing the PUT method: [/api/[id].ts] case "PUT": try { const user = await UserModel.findOneAndUpdate( { _id: id, }, { $set: req.body, ...

Can you provide guidance on how to access and utilize images within vue components?

I am currently in the process of creating a Single Page Application (SPA) using vue.js. I am facing an issue with assigning a background image to a div element by referencing a file in the path below: https://i.stack.imgur.com/qWjaU.png Although I am att ...

What is the best way to include a Web Service within an export variable in Angular 2 using TypeScript?

Is there a way to incorporate JSON data retrieved from the server into the export var HEROES: Hero[ ] function? Here is the link: https://angular.io/resources/live-examples/toh-5/ts/eplnkr.html In app/mock-heroes.ts, you will find the following data, im ...

TypeScript equivalent to Python's method for removing non-whitespace characters is achieved by

I understand that I can utilize .trim() to eliminate trailing spaces Is there a method to trim non-space characters instead? In [1]: str = 'abc/def/ghi/' In [2]: s.strip('/') Out[2]: 'abc/def/ghi' I am referring to the funct ...

In Angular, when a component is clicked, it is selecting entire arrays instead of just a single item at a

I am currently working on implementing a rating feature using Angular. This component will be used to rate different languages based on how proficient I am with them. My issue lies in the fact that when I click on one array element, it automatically selec ...

Since updating from Angular 16 to 17, I am experiencing a TypeScript compilation issue specifically related to 'openui5'

Everything was running smoothly in Angular16. I had "@types/openui5" : "1.40.4" listed in my dev-dependencies. Here is how it's configured in the tsconfig.json: { "compilerOptions": { "downlevelIteration": ...

Utilizing Input Value from Parent Component in Angular TypeScript File: A Guide

Is there a way to manipulate the @Input()items in the child-component.ts file before passing it to child-component.html? Currently experiencing an issue where I get an empty object when logging in the ngOnInit method. child-component.ts @Input()items: ...