Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Access to Object property using computed method in vue 3 veux shows undefined

I get undefined when I try to render the values of the computed property in the template using its property {{todo[0]['title']}} but {{todo[0]]}} renders the whole objects. I want to be able to render a todo using it property. Any assistance is well appreciated

const {
  createApp,
  ref,
  computed,
  onMounted
} = Vue;
const {
  createStore
} = Vuex;
const store = createStore({
  state: {
    alltodos: []
  },
  mutations: {
    setTodos(state, todo) {
      (state.alltodos) = todo
    }
  },
  getters: {
    allTodoGetters(state) {
      const sta = JSON.parse(JSON.stringify(state));
      return sta
    }
  },
  actions: {
    async fetchTodos({
      commit
    }) {
      const response = await axios.get('https://jsonplaceholder.typicode.com/todos');
      commit('setTodos', response.data);
    },
  }
});
const app = createApp({
  setup() {
    let alltodos = computed(() => store.getters["allTodoGetters"]);
    onMounted(() => {
      store.dispatch('fetchTodos')
    })
    return {
      alltodos
    }
  }
});
app.mount("#app")
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.30/vue.global.min.js"></script>
<script src="https://unpkg.com/vuex@4.0.0/dist/vuex.global.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.1/axios.min.js" integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div id="app">
  <div v-for="(todo, index)  of alltodos" :key="index">{{todo[0]}}</div>
</div>

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

If you want to show first item from array try like following snippet:

const {
  createApp,
  ref,
  computed,
  onMounted
} = Vue;
const {
  createStore
} = Vuex;
const store = createStore({
  state: {
    alltodos: []
  },
  mutations: {
    setTodos(state, todo) {
      (state.alltodos) = todo
    }
  },
  getters: {
    allTodoGetters(state) {
                                                // ๐Ÿ‘‡ return just the state you want
      const sta = JSON.parse(JSON.stringify(state.alltodos));
      return sta
    }
  },
  actions: {
    async fetchTodos({
      commit
    }) {
      const response = await axios.get('https://jsonplaceholder.typicode.com/todos');
      commit('setTodos', response.data);
    },
  }
});
const app = createApp({
  setup() {
    let alltodos = computed(() => store.getters["allTodoGetters"]);
    onMounted(() => {
      store.dispatch('fetchTodos')
    })
    return {
      alltodos
    }
  }
});
app.mount("#app")
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.30/vue.global.min.js"></script>
<script src="https://unpkg.com/vuex@4.0.0/dist/vuex.global.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.1/axios.min.js" integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div id="app">                           
  <div v-for="(todo, index) in alltodos"  :key="index">
           <!-- ๐Ÿ‘‡ show property you want -->
    <div v-for="(property, i) in todo">{{ property }}</div>
  </div>
</div>

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!