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

Viewed 265
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

1 Answers

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)
    })
  }
})
Related