【问题标题】:Vuex working wrongVuex 工作错误
【发布时间】:2018-03-13 06:12:24
【问题描述】:

我的 vuex 看起来像:

state: {
  loadedUsers: [
    { id: 10, classId: 1, name: 'X' },
    { id: 11, classId: 1, name: 'Y' },
    { id: 13, classId: 2, name: 'Z' }
  ]
}
getters: {
  loadedUsers (state) {
    return (classId) => {
      return state.loadedUsers.find((user) => {
        return user.classId === classId
      })
    }
  }
}

和我的计算:

computed: {
  users () {
    return this.$store.getters.loadedUsers(1)
  }
}

它只是返回 { id: 10, classId: 1, name: 'X' }

当我使用like时也

this.$store.getters.loadedUsers(this.$route.params.classid)

classid 到达但返回空

可能是什么问题?

【问题讨论】:

    标签: vue.js vuejs2 vue-component vue-router vuex


    【解决方案1】:

    这是因为Array.prototype.find() 返回匹配的first 元素的值。您应该改用Array.prototype.filter()。此外,您可以一层一层展开您的函数/方法调用:

    getters: {
      loadedUsers (state) {
        return classId => state.loadedUsers.filter((user) => {
          return user.classId === classId
        })
      }
    }
    

    【讨论】:

    • 但由于参数 classId 仍然返回空 -- 返回 user.classId === 1 -- 可以,但上面是返回空
    • @mak 然后你需要检查this.$route.params.classid 是否真的返回了一个classID。如果是这样,您是否将其解析为整数(即​​数字),因为您正在使用 === 进行严格比较。
    猜你喜欢
    • 2020-06-25
    • 2021-02-18
    • 2021-09-03
    • 2021-09-13
    • 2021-07-21
    • 2017-12-26
    • 2019-05-28
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多