Search code examples
vue.jsvue-componentvuex

How to call "this" in mapState in VUE component?


import { mapState } from 'vuex'

...mapState({
   user: (state) =>{
     return _.filter(state, data => {
       return _.includes(this.allUserIds, data.id)
     } )
   }
})

in this case, I won't call this.allUserIds in map state


Solution

  • Don't use () => {} function syntax because it is binding this too early.

    ...mapState({
      user(state) {  // <--- here
        return _.filter(state, data => {
          return _.includes(this.allUserIds, data.id)
        })
      }
    })