How can a component receive data from its parent element?

While diving into Vue.js, I encountered a puzzling issue - why isn't

<li>{{task.body}}</li>
appearing on the screen?

I've crafted a

<tasks v-for="task in tasks"></tasks>
component that requires access to data from its parent.

Check it out here: https://jsfiddle.net/pd03t1vm/

Here's a glimpse of the code:

<div id="app">
    <tasks v-for="task in tasks"></tasks>
</div>

<template id="tasks-template">
    <ul>
      <li>{{task.body}}</li>
    </ul>
</template>

And the JavaScript part:

Vue.component('tasks', {
    template: '#tasks-template',
});

new Vue({
    el: '#app',
    data: {
        tasks: [
            {body: 'Task 1 Something', completed: false},
            {body: 'Task 2 Something', completed: true},
            {body: 'Task 3 Something', completed: false},
            {body: 'Task 4 Something', completed: false}
        ]
    }
});

Answer №1

One issue you're facing is that you're instantiating the <tasks> components from the data in the root component without passing the current task into the <tasks> component. As a result, the component cannot access the task at all.

If you refer to the Vue.js guide, it explains how to pass data into a component using props:

To start, you must bind the current tasks to the prop (referred to as item) of the <task> component:

<div id="app">
  <tasks v-for="task in tasks" :item="task"></tasks>
</div>

Keep in mind that you use a : before the property name to bind the actual object.

Next, you'll need to define the item property in the <tasks> component:

Vue.component('tasks', {
  props: ['item'],
  template: '#tasks-template',
});

On a side note, your code generates four instances of the <tasks> component, in case you were curious. This may not align with what you initially intended your code to accomplish.

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

Instructions for updating the Modal Value using ajax

This is the script I use to retrieve the data <script> $(document).ready(function() { $('.tmpsurat').click(function() { var id=$(this).data('id'); var url ='{{URL('/cekSuratKelengkapan')}}/'+id; ...

Having issues with @react-three/drei in next.js environment

Having trouble using drei materials and other features like MeshWobbleMaterial, MeshDistortMaterial, or ContactShadows? You may encounter errors such as: react-three-fiber.esm.js:1383 Uncaught TypeError: Cannot read property 'getState' of null a ...

Adding a class to the body for a specific route in web development

I'm facing a situation where there is a class named: product-page-bottom-padding The requirement is to apply this class only to the /product/{slug} route for the body element. It should not be present in any other routes. Can you suggest how to mana ...

Struggling with overlaying Bootstrap modals on top of each other

This idea is inspired by the topic Multiple modals overlay I am working on developing a folder modal that allows users to either 1) open an existing file or 2) create a new file within each folder modal. To see how it functions, please run the code below ...

Animated text in ThreeJS

I'm interested in finding a way to animate text in ThreeJS that doesn't involve placing it directly on a plane. My ideal scenario would be to have the text appear as 2D, floating above a model. I've experimented with using divs positioned ou ...

Is it possible to utilize vue-meta without server-side rendering?

I have been experimenting with the vue-meta npm package for managing my meta tags. However, I am facing an issue where the content inside metaInfo() is not displaying correctly in Google search descriptions. metaInfo () { return { title: 'FA ...

Firebase Error: Page Not Found

I recently set up an Angular2 application and added Firebase using npm. I successfully imported it into my app.component.ts without any errors showing up in my text editor. The package.json file also indicates that Firebase is installed correctly. However ...

What steps are involved in integrating OpenCV into a JavaScript project?

After recently installing OpenCV via npm using this guide: https://www.npmjs.com/package/opencv I'm facing a simple question. How can I actually utilize the OpenCV library in my project? The site provides a face detection example code snippet: cv.r ...

Is it possible to filter JavaScript Array while ensuring that select options do not contain duplicate IDs?

I am utilizing two datatables with drag and drop functionality. Each row in the datatables contains IDs. When I drag a row from table 1 to table 2, the data is stored in an Array. I have an addArray function that pushes the IDs into the Array, filters out ...

Understanding the functionality of imports within modules imported into Angular

I have been scouring through the documentation trying to understand the functionality of the import statement in JavaScript, specifically within the Angular framework. While I grasp the basic concept that it imports modules from other files containing expo ...

What is the best way to repeatedly execute a function upon button click?

for (let index = 0; index < shoppingCenters.length; index++) { const mall = shoppingCenters[index]; locateAddress(mall); } $scope.locateAddress = function(mall) {} XHTML <ion-nav-buttons side="primary"> <button class="button" ng-click= ...

Learn to Generate a Mathematical Quiz with Javascript

For a school project, I am tasked with developing a Math Quiz which showcases questions one at a time. The questions vary in type, including Multiple Choice, Narrative Response, Image Selection, Fill in the blank, and more. I require assistance in creatin ...

Utilize the Webpack library and libraryTarget settings to establish our own custom library as a global variable

I currently have a library named "xyz" that is being imported as a node module from the npm registry. Now, I want to incorporate it as a library and make it accessible under the global name "abc". To achieve this, I plan to utilize webpack configuration. ...

Is there a way to upload a file using express/multer without triggering a redirect?

Note: Despite coming across this post, I couldn't find it helpful. Other related posts were focused on angular/react, which are not relevant to my current project. I have implemented a file upload feature that should provide a response indicating whe ...

Which one should I prioritize learning first - AngularJS or Laravel?

As a novice web developer, I am embarking on my first journey into the world of frameworks. After much consideration, I have narrowed it down to two options: AngularJS and Laravel. Can you offer any advice on which one would be best for me to start with? ...

To retrieve a property in Vue, you can use either the "this" keyword

Exploring Vue for the first time and navigating through accessing viewmodel data has me puzzled. When should I utilize this.property versus vm.$data.property. In this scenario with a table where I can select rows, there are methods in place to select all ...

Discover the best way to retrieve attribute values using jQuery

I'm struggling to retrieve the value of the attribute "_last_val" from my input, but I'm having trouble getting it. Here is what I have attempted: demo // Here is the HTML code <form method="post" action="" id="feedback_form"> <inpu ...

Enabling real-time notifications through Express 4 middleware with socket.io integration

I am in the process of developing a real-time notification system utilizing socket.io. Here is the current server-side code I have implemented: bin/www: var app = require('../app'); var server = http.createServer(app); var io = app.io io.attac ...

Launching the ngx Modal following an Angular HTTP request

Trying to trigger the opening of a modal window from an Angular application after making an HTTP call can be tricky. Below is the content of app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/pla ...

Using React to display data from a nested JSON object in a table

I am currently working on parsing a JSON object into a table using React. However, I am facing an issue with utilizing the .map() function to create a row for every unique combination of course code, name, transferable_credits, transferable_credits -> i ...