Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue3 setup variable not declared when accessing in template

In my vue file, my template has a div which uses v-for on an "items" array to load each element of the array visually. I have the "items" array defined in setup() using the composition API, and I return "items" in setup. Even with all this, when I try to render or load the page, it errors stating "main.js:30 ReferenceError: items is not defined"

In my template

<div v-for="product in items" :key="product.id" class="group relative">

In my <script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export default {
      setup() {
        let items = ref([]);

        onMounted(()=> {
        //here i retrieved data from firebase and pushed to items
        });

        return { items };

      }
    }

My error in dev tools is "main.js:30 ReferenceError: items is not defined"

(edited to include minimal reproducible example below)

<template>
  <div v-for="product in items" :key="product.id" class="group relative" >{{product.id}}</div>
</template>

<script >
import { ref,onMounted } from 'vue'
export default {
      setup() {
        let items = ref([]);
        onMounted(()=> {
        items.value = [{id:'a'}]
        });
        return { items };
      }
    }
</script>

>Solution :

It seems no error in your code, and I tested it here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading