Search code examples
javascriptvue.jsvuexvuetify.jsvue-router

How to access my state of array in another router page VUEJS, VUEX


I made a page with two routes one is the home page another is the config where you can decide what should be written to that container, now in the config panel I was able to get the input values, I put them to my state with map actions now I am getting an array with string values in it.

How can I access that array with mapGetters ? I link my code:

<template>
  <body>
    <div class="container">
      <h1 v-show="elementVisible" class="info">{{ message }}</h1>
    </div>
  </body>
</template>

<script>
  import moment from "moment";
  import { mapGetters } from "vuex";

  export default {
    name: "Home",
    data() {
      return {
        // message: this.store.state.message
        elementVisible: true
      };
    },
    computed: {
      ...mapGetters(["message", "sec"]),

      ...mapGetters({
        message: "message",
        sec: "sec"
      }),
      createdDate() {
        return moment().format("DD-MM-YYYY ");
      },
      createdHours() {
        return moment().format("HH:mm ");
      }
    },
    mounted() {
      this.$store.dispatch("SET_MESSAGE");
      this.$store.dispatch("SET_SEC");

      setTimeout(() => (this.elementVisible = false), this.sec);
    }
  };
</script>

so what I have to do is to put to that{{message}} template my message which I received from the config panel and which is in my state right now sitting there as an array of string, for example, ["hello", "how are you"] that's how they are sitting there, so how can I grab the first one('hello') and write it out as a clean string and not as ["hello"] if you know how to grab all of them would be even better.

(RightNow it's just putting that whole array to my template)

Maybe I should something rewrite in my storejs file? STOREJS:

const state = {
  message: [],
  // console.log(message);
  sec: +[]
  // other state
};
const getters = {
  message: state => {
    // console.log(this.state.message);

    return state.message;
  },

  sec: state => {
    return state.sec;
  }
  // other getters
};

const actions = {
  setMessage: ({ commit, state }, inputs) => {
    commit(
      "SET_MESSAGE",
      inputs.map(input => input.message)
    );

    return state.message;
  },

  setSec: ({ commit, state }, inputs) => {
    commit("SET_TIMEOUT", inputs.map(x => x.sec).map(Number));
    console.log(inputs.map(x => x.sec).map(Number));
    return state.sec;
  }
  // other actions
};
const mutations = {
  SET_MESSAGE: (state, newValue) => {
    state.message = newValue;
  },

  SET_TIMEOUT: (state, newSecVal) => {
    state.sec = newSecVal;
  }
  // other mutations
};

export default {
  state,
  getters,
  actions,
  mutations
};

what my homepage should do is that it writes out that message and there is a sec value which stands for the timeout, after that I want to continue with the second value in that array and when that times out I should want the third to be written out.. and so on.

Thank you!


Solution

  • Hello and welcome to Stack Overflow! Your message Array is being mapped correctly with mapGetters, but you're flattening it as a String when you put it inside the template with {{message}}, since the template interpolation logic covert objects to strings, the same as calling Array.toString in this case. You need to iterate it, i.e. using the v-for directive:

    <template>
      <body>
        <div class="container">
          <h1 v-show="elementVisible" class="info">
            <span v-for="m of message" :key="m">{{m}}</span>
          </h1>
        </div>
      </body>
    </template>
    

    Of course, if you only need the first item, you could show it directly using:

    <template>
      <body>
        <div class="container">
          <h1 v-show="elementVisible" class="info">{{message[0] || 'No message'}}</h1>
        </div>
      </body>
    </template>