What is the reason behind the inability to send an object from the parent component to a child component within a Vue 3 application?

I've encountered an issue while working on a Vue 3 app involving a component and its <User /> subcomponent.

Within App.vue:

<template>
  <Navbar />
  <Sidebar />
  <Content 
    title="Users"
  />
  <Footer />
</template>

<script>
import Navbar from './components/Navbar.vue'
import Content from './components/Content.vue'
import Sidebar from './components/Sidebar.vue'
import Footer from './components/Footer.vue'

export default {
  name: 'App',
  components: {
    Navbar,
    Content,
    Sidebar,
    Footer  
  },

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

Inside Navbar.vue:

<template>
  <a class="navbar-brand" href="#">My App</a>
  <User :users='users' />
</template>

<script>
import User from './User.vue'

export default {
  name: 'Navbar',
    components: {
        User
    }
}
</script>

In User.vue, I'm attempting to display the user's full name using {{ users.firstName }} {{ users.lastName }}, but encountering issues with typeof(users) returning object:

<template>
    {{ users.firstName }} {{ users.lastName }}
</template>

<script>
export default {
  name: 'User',
    props: {
    users: Object
  }
}
</script>

The Issue

An error "Uncaught TypeError: Cannot read properties of undefined (reading 'firstName')" is showing in the console.

Where did I go wrong?

Answer №1

//within the Navbar component

<template>

  <a class="navbar-brand" href="#">My Application</a>

    <User :userData='userInformation' />

</template>    

<script>
import User from './User.vue'

export default {
  name: 'Navbar',
    components: {
        User
    },
props:['userInformation']
}
</script>

//inside App.vue file

<template>
  <Navbar :userInformation="userData" />
  <Sidebar />
  <Content 
    title="Users List"
  />
  <Footer />
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Vue route parameters are not recognized within the function type

Seeking assistance on extracting parameters from my route in a vue page, I have the following implementation: <script lang="ts"> import { defineComponent } from 'vue'; import { useRoute } from 'vue-router'; export ...

Sending a XML file from a Vue application to an ASP.NET Core backend using axios

I'm encountering difficulties when trying to upload an xml file using axios to my asp .net server. Below is the code snippet I am using on the vue side to retrieve and upload the xml file: uploadXmlFile(file: any) { const rawFile = new XMLHttpRequ ...

I am looking to set a maximum display length of 5 items for every list in VueJS

For example, in Udemy's filter option, when you click on the "See More" button, it only shows 5 items. I tried creating a similar option using Vue, but when I clicked to see more, it displayed the entire list. I want to limit this list to only 5 item ...

In making reference to a particular subpage within a parent page

My website is designed with all inner pages linked to one master file. I'm looking to change the title font size on just one specific page, without affecting the rest of the pages that use the master file. Is there a way to target a specific page titl ...

How to Create Smooth Transitions for Text Arrays using jQuery's Fade In and Fade Out Features

I need to loop through an array of text and apply jQuery's fadeIn and fadeOut functions to each element. var greetings = ["hi, I'm", "bonjour, je m'appelle", "hallo, ich heiße"] The HTML structure I want is as follows: <h2><span ...

"Troubleshooting: Issues with message passing between popup.html and content.js in Chrome Extension

I'm facing a challenge in creating an Extension that transfers data from popup.html to the tab dome. Unfortunately, I am having trouble with getting the sendMessage function to work and I can't figure out why. I'm using the following guideli ...

Check for compatibility of overflow:scroll with mobile browsers

Is there an easy JavaScript method that works across different devices and libraries? I am looking to assign a class to the html element in order to enable scrollable containers on mobile devices when needed. I want to follow a similar approach to Modern ...

angular-bootstrap-mdindex.ts is not included in the compilation result

Upon deciding to incorporate Angular-Bootstrap into my project, I embarked on a quest to find a tutorial that would guide me through the download, installation, and setup process on my trusty Visual Studio Code. After some searching, I stumbled upon this h ...

What could be causing these warnings to pop up when I am utilizing the useEffect hook in React.js?

Having some trouble with React hooks and JS. I keep getting warnings about missing dependencies in my code, even after reading the documentation. It's all a bit confusing to me. ./src/CustomerList.js Line 32:6: React Hook useEffect has a missing d ...

Using Vue.js to retrieve this.$route.params within the mounted hook functions

I am attempting to sort through a movie table by director ID. A movie's structure looks like this: { id: 1, title: "They All Lie", releaseYear: 1989, director: { id: 18, ...

Using b-icon with the <td> tag in VueJS

I am looking to incorporate HTML content within a table data element using VueJS. Below is an illustration of my situation: <template> <div> <div v-if="someObject.properties" style="margin-top: 20px;" class="table-responsi ...

Retrieve specific JSON information

Below is an example of JSON data: [{ "RM_Name": "Russ Martin", "Division": "East", "RM_Phone": "(603) 491-1259", "RC_Name": "Jacob Sucoff", "RC_Phone": "(800) 247-4154 x3403", "States_Covered": "MT,VT, NH, ME (all firms)", "La ...

Unable to properly execute Fetch Delete Request

When using the Fetch API, I am sending this request: const target = e.currentTarget; fetch(target.href, { method: 'delete', }) .then(res => console.log(22)) .catch(err => console.log(err)); In addition, here is the middleware that manag ...

When the PHP response is received by AJAX, an error occurs due to a failed JSON parsing request

Every time I try to run my small JavaScript code with an AJAX call to PHP, it keeps coming back with a JSON parser error. In the PHP code, I can see that the JSON is populated with an array like this: json encode: {"Year":"2012","Make":"Ford","Model":"Tau ...

Transferring a CSV file to the server from a React application using multi-part form

In order to post a CSV file to an API using React, I have attempted to do so in multipart form. While many tutorials and websites suggest using the fetch() method for sending files to a server, I am encountering some challenges. The issue lies with my RES ...

What is the best way to access a variable from a class in ES6?

Hey there, I'm having trouble accessing the variable from the KeyCodeClass. Let me walk you through what I've tried so far: Attempt 1 class KeyCodeClass1 { constructor() { this.space; } KeyDown(e) { if (e.keyCode == 3 ...

The Angular 2 rollup AoT compilation results in a larger build size compared to the standard JiT build

I'm facing an issue with reducing the weight of my app during the building process. I am using Angular 2 seed as a starting point. https://github.com/mgechev/angular-seed When I run: npm run build.prod my app.js file size is 1.5MB. However, when I ...

navigate to the subsequent array element by clicking in JavaScript

My apologies if my explanation is unclear or if the code seems complicated, I'm still in the learning process. I am attempting to showcase data that is stored in an array. The objective is to have this data displayed upon clicking a button and then sh ...

What is the best way to hide the background of an extension's HTML?

Currently, I am working on developing a Chrome extension for a university project. However, I am facing challenges in making the background or body of the extension's HTML completely transparent to achieve a cleaner interface. The issue specifically l ...

How come my dynamic source path doesn't function correctly unless I add an empty string at the end of it?

Recently, I encountered an issue while using Vue.js to dynamically create a source attribute using an object's properties. Here is the code snippet where I faced the problem: <img :src='"../assets/" + project.image.name + "." + project.image. ...