How is the correct way to work with Vuex and typescript?

Vuex is perfectly compatible with typescript using these vuex imports: import {GetterTree, MutationTree, ActionTree} from “vuex” The example below shows the easiest and most complete way to use vuex in typescript. Principal store file: import Vue from ‘vue’ import Vuex from ‘vuex’ import { GetterTree, MutationTree, ActionTree } from “vuex” import MySubModule from ‘@/store/submodule’ Vue.use(Vuex) … Read more

Why Pinia vs Vuex for Vue 3? [closed]

In short, Pinia’s API is much simpler and intuitive. It makes using stores a breeze, even for junior devs. It brings the benefits of Composition API, but has a structure which greatly resembles the Options API, which you’re probably familiar with: has a reactive state, equivalent of data function in Options API has getters, equivalent … Read more

Vue/Vuex unknown action type

Try this: import { mapGetters, mapActions} from ‘vuex’ computed: { …mapGetters({ companies: ‘company/allCompanies’ }) } methods: { …mapActions([‘company/getCompanies’, ‘company/getEmployees’]) }, mounted() { this[‘company/getCompanies’](); }, But, I like to do namespacing as done below, but it has an issue, refer the issue here. methods: { …mapActions(‘company’, [‘getCompanies’, ‘getEmployees’]) }, mounted() { this.getCompanies(); this.getEmployees(); },

How to get vuex state from a javascript file (instead of a vue component)

It is possible to access the store as an object in an external js file, I have also added a test to demonstrate the changes in the state. here is the external js file: import { store } from ‘../store/store’ export function getAuth () { return store.state.authorization.AUTH_STATE } The state module: import * as NameSpace … Read more

Async/Await with Vuex dispatch

Change this: getProducts({commit}, type) { axios.get(`/api/products/${type}`) .then(res => { let products = res.data; commit(‘SET_PRODUCTS’, {products, type}) }).catch(err => { console.log(err); }) }, To this: getProducts({commit}, type) { return axios.get(`/api/products/${type}`) .then(res => { let products = res.data; commit(‘SET_PRODUCTS’, {products, type}) }).catch(err => { console.log(err); }) }, Should work. axios.get returns a promise. You would need to … Read more

Pass params to mapGetters

If your getter takes in a parameter like this: getters: { foo(state) { return (bar) => { return bar; } } } Then you can map the getter directly: computed: { …mapGetters([‘foo’]) } And just pass in the parameter to this.foo: mounted() { console.log(this.foo(‘hello’)); // logs “hello” }

tech