Interested in creating a list using vue.js?

Whenever I attempt to show a list from an array of data, I encounter the following error message:

cannot read property 'name' of undefined
.

HTML

<div id="list">
  <myitems v-for="item in list" v-bind:my="item">
</div>

JavaScript

In my JavaScript code snippet, I define a Vue component called 'myitems' with a prop of 'my', and a template that displays information related to each item.

Next, I create a new instance of the 'myitems' component and pass in an array of objects containing name and age properties. However, when attempting to mount the component onto the element with the id 'list', the aforementioned error occurs.

I suspect that there may be an issue with how I am iterating over the items using 'v-for' and binding them to the 'my' prop. Any suggestions on what steps to take next would be greatly appreciated.

Answer №1

It seems like the issue lies within your html code. The <myitems> tag must be contained within the v-for directive in order for it to function properly. Make sure not to declare the <myitems> tag separately. You can try revising your code as shown below:

Updated HTML

<div id="list" v-for="item in list" >
  <myitems  v-bind:my="item"></myitems>
</div>

Answer №2

Hey, looks like there are a few issues in your code that need addressing.

First off, it seems like you haven't defined the root Vue instance correctly. It should be set up like this:

new Vue({
  data: {
    list: [
      {name: 'myName', age: 25}
    ]
  }
}).$mount("#list");

Your component registration is correct, so keep it as is:

var items = Vue.component('myitems', {
  props: ['my'],
  template: `<li>{{my.name}} {{my.age}}</li>`
});

You also forgot to wrap your component within a ul tag since your template returns an li.

<div id="list">
  <ul>
    <myitems v-for="item in list" v-bind:my="item"></myitems>
  </ul>
</div>

Check out this working example: https://jsfiddle.net/am5Ldp9k/

Answer №3

Your code has a few issues that need to be addressed. Firstly, there is no need to assign a variable to Vue components that you register. Once you register them with Vue.component, they will automatically be available to you.

Vue.component('myitems', {
  props: ['my'],
  template: '<li>{{my.name}} {{my.age}}</li>'
});

Secondly, it is important to have a root component. This can be achieved by initializing a new Vue instance using new Vue():

new Vue({
  el: '#list',
  data: {
    list: [{ name: 'myName', age: 25 }]
  }
});

You must provide an element ID or class as the root element, along with any data that needs to be passed down to child elements. Once you make these adjustments, your code should work correctly. Feel free to refer to this jsfiddle for a functional example: https://jsfiddle.net/coligo/49gptnad/

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

Retrieve specific information from a JSON file and save it in an array using Node.js/JavaScript

My goal is to extract all log details from a JSON object, but the issue is that the key for each user is constantly changing. let userJsonObject = { "user_1":{ "id":"user_1", "log":"de-data", ...

Unable to create a new user account

I have been working on developing an e-commerce platform using the MERN stack, but I have encountered an issue with registering new users. The system only allows me to register the first user successfully after clearing the database, but subsequent registr ...

How can I detect a DOM element mutation based on a CSS selector, and if this is possible, how can it be accomplished?

Imagine there's a website with a specific HTML element. It seems that this element has the same class during the DOMContentLoaded event as it does during the load event. However, after the load event, this class (and possibly the ID and other HTML att ...

The RemoveEventListener function seems to be malfunctioning within Angular2 when implemented with TypeScript

I am currently incorporating three.js into Angular2. The code I am using is quite straightforward, as shown below. this.webGLRenderer.domElement.addEventListener('mousedown', ()=>this.onMouseDown(<MouseEvent>event), false); this.webGLR ...

How can one retrieve the set-cookie value in a <meta http-equiv> tag using Node.js?

When working with Node.js, I am encountering a scenario where I need to detect instances of a cookie being set in a response so that I can make changes to it. There are two ways in which cookies are being set: Through the use of the set-cookie HTTP heade ...

What steps should I take to ensure my clock stays in sync with my runTime function?

I am developing a mini digital clock project with the ability to mimic a physical clock. The clock is activated by using a power button to switch it on and display the current time. It should also be able to turn off and show an empty screen. However, th ...

Guide to automating tests on MAC using Protractor with Appium and IOS-Simulator

Is there a way to run automated tests using Protractor (or WebDriverJS) on a MAC with Appium and the IOS Simulator? We have been unsuccessful in running tests with the following configuration file, although it works fine with Selenium 2.0 - WebDriver. He ...

Troubleshooting: ng-disabled feature is not properly functioning with Bootstrap buttons

I am currently using a combination of bootstrap.js and angular js in my project. The code snippet I have is as follows: //snippet from the controller $scope.isWaiting = true; $scope.promise = $http.get("voluumHandler.php?q=campaigns&filter=traffic-sou ...

Experimenting with two distinct click events using vue-test-utils

I ran a test to check if two methods are being called when clicking on two different buttons. The result showed that the mock method for the second button click event was not triggered. Below is the template from JobsSearch.vue: <template> < ...

Angular Material Clock Picker for 24-Hour Time Selection

Struggling to find a time picker component that supports the 24-hour format for my Angular 14 and Material UI application. Can anyone help? ...

JavaScript: Targeting elements by their tag name within a designated container

When working with PHP, it is possible to use the getElementsByTagName method on any DOM object. However, this concept does not seem to exist in JavaScript. For example, if we have a specific node stored in the variable detailsNode, attempting to use detai ...

What are the methods for altering the material of a glTF model using THREE.js?

I've created a model using Blender and baked all the lighting and textures. However, when I import it into THREE.js in .glb format, it automatically uses the standard material. While this may work in certain cases, my concern is that I want to retain ...

Inspect each chart for information and conceal any that are empty

Currently, I am attempting to analyze my highcharts graphs for data during loading and each redraw. I have successfully implemented the following code: (function (H) { var chartPrototype = H.Chart.prototype; // Display or hide graph based on da ...

What is the process of molding items?

Here is an object that I have: { "id": "Test", "firstname": "Test", "lastname": "Test", "age": 83 } However, I only want to return the object with this value: { "id&quo ...

Guide on importing a React.Component from a Content Delivery Network (CDN) and displaying it within a separate React

Note: None of the solutions provided are effective [DO NOT REMOVE THIS NOTE] Here's a simple question for you - I have a project named Project Y, created using the command npx create-react-app react-project. Now, in the App.js file of Project Y, I h ...

Leveraging the Scroll feature in Bootstrap for smooth scrolling

Seeking assistance with implementing scroll in Bootstrap 5 on my child component (ProductList.vue). Can anyone guide me on how to integrate the code? I have been searching for a solution without success. Below is the Bootstrap 5 code on the child component ...

Is it possible for two-way binding to function in index.html within Angular 4?

Does two-way binding function in index.html? I have some links and metadata in index.html. How can we define head parameters in a component? <head> <meta name="og:title" content={{titleValue}}> <meta name="og:url" content={{urlValue}}> & ...

Angular Material Password Confirmation

Currently, I am working on implementing user authentication using Angular Material. Specifically, I am focusing on displaying the appropriate error message when the confirmed password does not match the initially entered password. Below is the snippet of ...

If a user touches outside of the scroll component on mobile, the scroll in the popup freezes

I have a webpage that includes two scrollable lists: one in the main body and another in a popup window. To prevent scrolling of the main body when the user scrolls inside the popup, I am using a standard solution recommended in this Stack Overflow post ab ...

Ways to differentiate between an angular element and a jQuery element

In order to implement a feature where clicking outside of a dropdown hides it within a directive, I have the following code: $(document).click(function(e) { var selector = $(e.target).closest('.time-selector'); if (!selector. ...