vuexvuejs3vue-composition-apivuex4vue-script-setup

How to use Vuex mapGetters with Vue 3 SFC Script Setup syntax?


I'm refactoring component from regular Vue 3 Composition API to Script Setup syntax. Starting point:

<script lang="ts">
import { defineComponent, computed } from 'vue';
import { mapGetters } from 'vuex';

export default defineComponent({
  name: 'MyCoolBareComponent',
  computed: {
    ...mapGetters('auth', ['isAdmin']),
  },
});
</script>

Current Vue v3 migration documentation, SFC Composition API Syntax Sugar (< script setup >), links to this RFC page: https://github.com/vuejs/rfcs/pull/182

There is only one example for using computed reactive property:

export const computedMsg = computed(() => props.msg + '!!!')

As there is no current Vuex 4 documentation available that is mentioning <scrip setup>, it remains unclear to me how I should be using mapGetters when using this syntax? Or what is the correct way of going about this with Vuex 4?


Solution

  • So far this syntax seems to be working. However, I'm hoping that Vuex would develop a cleaner way for exposing computed getters for template.

    If you know a better way, we'd love to hear!

    <script setup lang="ts">
    import { mapGetters } from 'vuex';
    
    export const name = 'MyCoolBareComponent';
    
    export default {
      computed: {
        ...mapGetters('user', ['profile', 'roles']),
      },
    };
    </script>