微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在计算方法和mapState中的数组中分配动态参数

如何解决如何在计算方法和mapState中的数组中分配动态参数

您好,这是我的计算方法

computed:
    mapState({
    aftermovie: (state) => state.festival.aftermovies[id]
  }),id: {
      set() { return this.$route.params.id}
    },

如果我将state.festival.aftermovies [0]放上了,但是如果我尝试在网址中获取ID,则ID未定义,请您帮忙

解决方法

我认为问题是您无法访问this中的mapState或其他计算的属性,请尝试以下操作:

computed: {
  ...mapState({
    aftermovies: (state) => state.festival.aftermovies
  }),id() {
    return this.$route.params.id
  },aftermovie() {
    return this.aftermovies[this.id]
  }
}
,

非常感谢,确实我们无法在 mapState 中访问 this ,这是功能性解决方案:

data() {
    return {
    // -1 because the aftermovie index is 1 and the index in the array starts at 0
      id: this.$route.params.id - 1
    }
  },computed:
    mapState({
      aftermovies: (state) => state.festival.aftermovies
    }),

最后访问数据

<template>
  <div>
    <div class="container text-center">
      <div>
        {{ aftermovies[this.id].id }}
        {{ aftermovies[this.id].name }}
        {{ aftermovies[this.id].video }}
      </div>
    </div>
  </div>
</template>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。