4

I'm trying to use mapGetters from Vue.js but instead use NPM to install I used Vue CDN like this:

<script src="https://unpkg.com/[email protected]/dist/vuex.js"></script>

I can't do the import as usual:

import {mapGetters} from 'vuex';

And neither use mapGetters without import.

export default {
computed: mapGetters([
  'doubleCounter',
  'realCounter'
])}

How can I use mapGetters using Vue CDN?

2 Answers 2

12
export default {
  computed: Vuex.mapGetters([
    'doubleCounter',
    'realCounter'
  ])
}

export default {
  computed: {
    ...Vuex.mapGetters([
      'doubleCounter',
      'realCounter'
    ]),
    otherComputedProperty () {
      // implementation
    }
  }
}
1

I had run into this exact issue and this answer was perfect. The reason I replying is because in google searches you are unlikely to find this unless you include CDN in your search. With that included it is a top result as it should be. I hope others will find this in the future if they are looking for solutions when they are searching for how to use mapGetters or mapState or mapActions or mapMutations and they add the line

import { mapMutations } from 'vuex'

only to get the error in the console

Uncaught SyntaxError: Cannot use import statement outside a module

I think the answer would be fairly obvious to someone familiar with including via CND instead of via NPM, but for a relatively new cut-and-paste jockey, like myself, I was struggling to solve this problem. I didn't even think to include cdn in my google search. Only after asking in a different forum did someone suggest this post and/or including it in the search did I find this easy and clear solution.

Hopefully by posting this, the indexing will eventually include this in some of the searches I did without cdn, for the next explorer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.