Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mapState function in typescript syntax when using vuex?

I'm using typescript syntax in my vuejs project that integrated with vuex. I want to use mapState method as computed in my .ts file but I got a syntax error. Currently I am using docs suggested syntax for computed function, I mean:

 get counter() {
   return  this.$store.state.count;
 }

If you read the Vuex docs you will see that using Vuex in this way instead of using mapState is very repetitive. Using mapState is very easy and useful in large applications. I want to use mapState in my Typescript component and I don't know the right way. I've tried the way below to use the mapState function and it didn't work.

get mapState({
  counter:count
});

// or

get mapState(['name', 'age', 'job'])

I'd be grateful if someone could help me.

like image 734
hamid hasani Avatar asked Jul 26 '18 08:07

hamid hasani


People also ask

What is the use of mapState in Vuex?

Mapping in Vuex enables you to bind any of the state's properties, like getters, mutations, actions, or state, to a computed property in a component and use data directly from the state. Although we can get the job done with this. $store.state.user.data.name , we can use a map helper to simplify it to this.

What is $store in Vue?

In Vue, as well as other front end frameworks like React, a store is a centralized location to keep up with data that is available across all the application components. A logged in user is a perfect example of data that belongs in a store.

Is Redux same as Vuex?

And there is the main difference between them - while Redux uses reducers Vuex uses mutations. In Redux state is always immutable, while in Vuex committing mutation by the store is the only way to change data.


2 Answers

You may call mapState within the Component annotation:

import { Component, Vue } from 'vue-property-decorator';
import { mapState } from 'vuex';

@Component({
  // omit the namespace argument ('myModule') if you are not using namespaced modules
  computed: mapState('myModule', [ 
    'count',
  ]),
})
export default class MyComponent extends Vue {
  public count!: number; // is assigned via mapState
}

You may also use mapState to create new computeds based off of your state:

import { Component, Vue } from 'vue-property-decorator';
import { mapState } from 'vuex';
import { IMyModuleState } from '@/store/state';

@Component({
  computed: mapState('myModule', {
    // assuming IMyModuleState.items
    countWhereActive: (state: IMyModuleState) => state.items.filter(i => i.active).length,
  }),
})
export default class MyComponent extends Vue {
  public countWhereActive!: number; // is assigned via mapState
}
like image 193
Jeremy Frey Avatar answered Oct 20 '22 20:10

Jeremy Frey


Easier using JS Spread syntax:

<template>
  <div class="hello">
    <h2>{{ custom }}</h2>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { mapState } from 'vuex';

@Component({
  computed: {
    ...mapState({
      title: 'stuff'
    }),
    // other stuff
  },
})
export default class HelloWorld extends Vue {

  title!: string;

  public get custom():string {
    return this.title;
  }
}
</script>

Your store:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    stuff: 'some title',
  },
  mutations: {

  },
  actions: {

  },
});
like image 24
Edgar Quintero Avatar answered Oct 20 '22 22:10

Edgar Quintero