Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
How do I make a variable available to a grandchild component in this Vue 3 application?

I am working on a Vue 3 app. I have run into a problem working with a parent component, a child component and a grandchild component.

In Main.vue (parent compenent):

<template>
    <div class="main">
        <h1>{{ title }}</h1>
        <Filters
         :usersData='users' 
        />
    </div>
</template>

<script>
    import Filters from './Ui/Filters'
    export default {
      name: 'Posts',
      components: {
        Filters
      },
      props: {
        title: String,
      },
    }
</script>

In the child compenent Filters.vue:

<template>
    <label>Filter by author</label>
    <MyDropdown
        label="All authors" 
        :usersData='users'
        />
</template>

<script>
    import MyDropdown from './MyDropdown'
    import MyButton from './MyButton'

    export default {

      name: 'Filters',

      components: {
        MyDropdown,
        MyButton
      },

      props: {
        usersData: Object
     },

    data() {
        return {
          url: 'https://jsonplaceholder.typicode.com',
          users: [],
        }
    },
    async mounted() {
        // Users
        await this.axios.get(`${this.url}/users`).then((response) => {
          if (response.status == 200) {
            this.users = response.data;
            console.log(this.users); // outputs the users correctly
          }
        }).catch((errors) => {
          console.log(errors);
        });
      }
    }
</script>

Tn the grandchild component MyDropdown.vue:

<template>
  <div class="dropdown">
    <button type="button" class="btn btn-sm dropdown-toggle" data-bs-toggle="dropdown">
       {{ label }}
    </button>
    <ul v-if="usersData.length" class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Jane Doe</a></li>
        <li><a class="dropdown-item" href="#">John Doe</a></li>
    </ul>
  </div>
</template>

<script>
    export default {
      name: 'MyDropdown',
      props: {
        label: String,
        usersData: Object
      }
    }
</script>

The problem

As long as v-if="usersData.length is present, I get this error in the console:

Cannot read properties of undefined (reading 'length')

Where is my mistake?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

First of all, if it is just avoiding your error in the MyDropDown grandchild component you can just add a ? after the usersData variable like v-if="usersData?.length". This will remove your Can't read length of undefined error.

Next is the way you're trying to pass your props from parent to the child component. You are passing your users data to usersData prop of the Filters component tag but are trying to get the props as filterData in the component, which will not work as expected. So users is considered with an undefined value and is being the same into your grandchild which is causing an error in your grandchild i.e. MyDropDown component.

Try mapping the prop names properly and avoid variable name repetition with that of the prop names inside any component.

If you are trying to send data from parent to grandchild directly you use the Provide/Inject feature of Vue.js.
Find more about it here Provide/Inject | Vue.js

about 4 years ago · Juan Pablo Isaza Denunciar

0

v-if="usersData?.length > 0"

try this or

v-if="usersData.length > 0"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda