Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VUE3: Property ... was accessed during render but is not defined on instance

Hello I'm writing a simple Vue.js app and I want to use the v-for directive on the component HelloWorld using data from my items array.

Here is the code:

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld
    v-for="(item, index) in items"
    :key="index"
    :title="item.title"
    :msg="item.msg"
  />
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    let items = [];
    for (let index = 0; index < 5; index++) {
      items.push({ title: `Test ${index}`, msg: `Test msg ${index}` });
    }
    return items;
  },
};
</script>

This is the error that I get:

Property "items" was accessed during render but is not defined on instance. at <App>

How can I fix this? I tried using setup() and ref but it didn't work.

like image 894
Lorenzo Andreasi Avatar asked Oct 20 '25 19:10

Lorenzo Andreasi


1 Answers

You should return items as nested value of an object :

  data() {
    let items = [];
    for (let index = 0; index < 5; index++) {
      items.push({ title: `Test ${index}`, msg: `Test msg ${index}` });
    }
    return {
         items,
         //other properties
      };
  },
like image 151
Boussadjra Brahim Avatar answered Oct 23 '25 11:10

Boussadjra Brahim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!