【问题标题】:How to map results within vue computed property如何在 vue 计算属性中映射结果
【发布时间】:2019-06-21 03:55:02
【问题描述】:

我们的应用有多个位置,每个位置都有一个由 a、b、c 或 d 组成的 accessGroup。 我们的用户有一组 accessGroups ['a', 'c'] 等。

在我们的位置页面上,myLocations 应返回 location.accessGroup 包含在其 accessGroup 数组中的位置。

computed: {
  ...mapState(['currentUser', 'locations', 'userProfile']),
}
myLocations: function() {
    return this.locations.filter((location) => {
      return location.accessGroup === this.userProfile.accessGroup
    })
  },

【问题讨论】:

    标签: vue.js computed-properties


    【解决方案1】:

    看起来您正在将字符串与数组进行比较。试试这个:

    // assuming userProfile.accessGroup is an array of strings like ['a', 'c'] etc
    // and location.accessGroup is a string: 'a', 'b', 'c', or 'd'
    myLocations () {
      return this.locations.filter((location) => {
        return this.userProfile.accessGroup.indexOf(location.accessGroup) >= 0
      })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2019-12-18
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 2016-10-07
      相关资源
      最近更新 更多