Issue with passing props from parent component to child component in Vue.js not functioning

As a newcomer, I have been assigned a project that involves using Vuejs.

In this project, there is a page that utilizes a component called DashboardCard. Each DashboardCard receives 2 props - val and icon from the parent component.

https://i.stack.imgur.com/kADo3.png

I have declared the props in the DashboardCard component (child component), but for some reason, not all prop values are being passed and assigned.

https://i.stack.imgur.com/JLt3x.png

Now, I am encountering an error related to this issue:

https://i.stack.imgur.com/eNa9u.png

UPDATE: Upon checking the Vuejs devtool log, it appears that the props were indeed passed correctly. https://i.stack.imgur.com/tdCkv.png

Answer №1

One of the reasons for the issue is that you are trying to access the props before they hold any value. There are two ways to resolve this:

Within the parent component (Dashboard.vue):

Try passing the values like

:icon="'@/assets/sale.png'"
and :val="'$700'"

In the child Component (DashboardCard.vue):

Method-1:

Create two data variables and set them with the prop values using a watch handler

export default {
 components: {DatePicker},
 props: {
   icon: {
     type: String,
     default: ''
   },
   val: {
     type: String,
     default: ''
   }
 },
 data() {
  return {
    iconPath: '',
    amtVal: ''
  };
 },
 watch: {
  icon(newVal, oldVal) {
    this.iconPath = newVal;
  },
  val(newVal, oldVal) {
    this.amtVal = newVal;
  }
 }

Use your local data variables in the template like

<span>{{amtVal}}</span>

and

<img :src="require(iconPath)" alt="icon" />

Method-2:

Utilize a computed property

export default {
 components: {DatePicker},
 props: {
   icon: {
     type: String,
     default: ''
   },
   val: {
     type: String,
     default: ''
   }
 },
 computed: {
   getIcon() {
     return this.icon;
   },
   getVal() {
    return this.val;
   }
 }

Incorporate these computed properties in the template like

<span>{{getVal}}</span>

and

<img :src="require(getIcon)" alt="icon" />

Answer №2

Don't forget to close your script tag with </script> in the DashboardCard component as Vue won't recognize the component properties without it. I initially thought there was more code below, but it wasn't until I tested it in a sandbox environment.

Keep in mind that in the Vue devtools extension, props should be listed under the props tab, which doesn't seem to be the case for you.

I recommend using snippets to prevent these types of typos. Personally, I use Vetur which not only helps with avoiding errors but also provides syntax highlighting—a very useful extension indeed.

Check out Vetur here for an enhanced coding experience!

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

removing a specific cookie from the browser's cookies using jQuery

This block of code displays a URL along with a delete image for removing a cookie. The add and display functions are working fine, but I'm stuck on how to implement the delete function. function backLinks(){ var pathname = window.location; va ...

How to successfully configure environment variables in Next.js and deploy them on Netlify

An issue arose after deploying my Next.js application to Netlify using Git. I utilize a .env.local file to store the backend route URL that is used across the entire app for fetch requests. However, post deployment, the process.env.NEXT_PUBLIC_BACKEND_ROUT ...

Unable to allocate a second item to an existing one

Encountering an unusual issue while trying to assign an item a second time. Initial scenario: I am working with a jqxTree containing various items as shown below: - apple - oracle - microsoft When I drag and drop one item into another, the structure loo ...

Maintaining state value during client-side navigation in NextJs with Next-Redux-Wrapper

Currently, I am working on resolving the hydration issue that occurs when using wrapper.getServerSideProps. The problem arises when I reroute with the existing setup and the store gets cleared out before adding new data. This leads to a blank page as essen ...

Expanding the use of tagged template literals for passing additional arguments

Currently, I am utilizing styled-components and creating components through their tagged template literal syntax like this: const Button = styled.button` background-color: papayawhip; border-radius: 3px; color: palevioletred; ` In a specific scenar ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...

Using TypeScript, you can pass an object property name as a function argument while ensuring the type is

How can I define a type in a function argument that corresponds to one of the object properties with the same type? For instance, if I have an object: type Article = { name: string; quantity: number; priceNet: number; priceGross: number; }; and I ...

Add a style to every div except for the final one

I've attempted to add a unique style to all divs with the same class within a parent div, except for the last one. Strangely, my code doesn't seem to work as expected for this particular case. Can anyone spot what I might be overlooking? Right no ...

Error: An unidentified type was encountered that is not a functioning element within an anonymous PHP JavaScript function

Hello everyone, I am seeking help with a JavaScript error that is causing some trouble. Whenever I try to sort a table and implement pagination in PHP, I get an error message in the console stating "Uncaught TypeError: undefined is not a function." Despite ...

Using Vue.js to process JSON data

My issue lies within this JSON data. I am trying to consume only the first element of the array using Vue.js 2 in order to display it. I was able to achieve this successfully using the console, but not with Vue.js. This line of code in the console works: ...

Can you explain the distinction between compiled and interpreted programming languages?

Despite my efforts to research the topic, I am still confused about the distinction between a compiled language and an interpreted language. It has been mentioned that this is one of the distinguishing factors between Java and JavaScript. Can someone ple ...

Can Jquery mobile detect drag gestures?

Is there an event in Jquery Mobile that allows you to hide/show a div by dragging it, similar to the effect seen on phones? I have searched on different platforms and found that using Jquery UI is the closest solution. Is it possible to achieve this only ...

Solution: Replace occurrences of 'window' with 'global' for improved

Currently experimenting with a component on runkit.com. The platform is advising me to make a change based on the image provided... Solution: Use global instead of window. Since RunKit operates in a node environment, browser-specific features like win ...

How to target the last child in Flexbox using CSS order property

Is there a way to correctly detect the last child after rearranging divs using flex order? Even though I have set the order in the CSS, it is still recognizing the last child based on the DOM tree. Since the items are dynamic, we can't rely on nth-chi ...

When a JSON object is handled as a string

The JSON returned from my Ajax call looks like this: returnedData = "[ { id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London', orders: [ { product: ...

Struggling to integrate Nuxt.js, Express, and a 3rd-party API seamlessly

I've been attempting for weeks to integrate Nuxt.js + Express (using create-nuxt-app) with a 3rd-party API without success. No matter what I attempt, nothing seems to work. None of my server-side console statements are triggered, only the front-end on ...

Convert the entire webpage to JSON format instead of traditional HTML using Nuxt.js

My current Nuxt.js application is set up to serve HTML pages using layouts, pages, routes, and components. I'm wondering if it's possible to render a single route or page as a standard JSON response without any HTML tags - just pure JSON. Here ...

Mapping the changes in the checkbox of a material tree node

Check out this demo on StackBlitz: Tree View I'm having issues with the tree not displaying as desired. I would like it to look like this: Manager Sublist Manager 1 Manager 2 Manager 3 Could you please review and provide some advic ...

Ensure Angular JS includes a space or special character after applying a currency filter

Is there a way to include a space or special character after the "₹" symbol? Desired output: ₹ 49 Current output: ₹49 HTML - {{cart.getTotalPrice() | currency:"₹"}} ...

Trouble with the display none function

I am trying to achieve the following: If my shopping cart is empty, I want another div to display over the top of it. Instead of seeing the cart, users should see a message saying 'your cart is empty'. Currently, I have set the other div to dis ...