0

你好,这是我的计算方法:

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

如果我把 state.festival.aftermovies [0] 它工作但如果我尝试在 url 中获取 id,id 是未定义的,请你帮帮我

4

2 回答 2

0

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

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>

于 2020-10-30T07:17:04.227 回答
0

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

computed: {
  ...mapState({
    aftermovies: (state) => state.festival.aftermovies
  }),
  id() {
    return this.$route.params.id
  },
  aftermovie() {
    return this.aftermovies[this.id]
  }
}
于 2020-10-30T04:57:56.593 回答