vue.jsvuexcomputed-propertiesmapstatetoprops

how to assign a dynamic parameter in an array in the computed method and mapState


hello here is my computed method :

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

if i put state.festival.aftermovies [0] it works but if i try to get id in url, id is undefined, can you help me please


Solution

  • thank you very much yes indeed we can not access this in mapState here is the functional solution:

    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
        }),

    and finally to access the data

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