如何在VUE组件中调用mapState中的“this”?

Lyh*_*Din 3 vue.js vue-component vuex

import { mapState } from 'vuex'

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

在这种情况下,我不会在地图状态下调用 this.allUserIds

Dec*_*oon 6

不要使用() => {}函数语法,因为它绑定得this太早。

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