Issue with Vue.js v-for not rendering additional li tags

I am working with a ul that contains items generated from a loop, followed by additional li elements.

<ul>
  <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" />
  <li :key='"new_item"'>
    <input placeholder="What needs to be done?" type="text" v-model="new_todo" >
    <button v-on:click="add_todo()">Add</button>
  </li>
</ul>  

When the extra li comes after the looped items, it does not display as expected. However, when I place the extra li before the loop, it works fine.

<ul>
  <li :key='"new_item"'>
    <input placeholder="What needs to be done?" type="text" v-model="new_todo" >
    <button v-on:click="add_todo()">Add</button>
  </li>
  <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" />
</ul>  

I suspect there may be an issue with the key attribute, but I'm struggling to identify the exact problem.

You can view the full code here.

Answer №1

One of the challenges with Vue components is that they require a closing tag and won't function correctly with self-closing tags.

To address this issue, you can structure your code like this:

<ul>
  <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index"></todo-item>
  <li :key='"new_item"'>
    <input placeholder="What needs to be done?" type="text" v-model="new_todo" >
    <button v-on:click="add_todo()">Add</button>
  </li>
</ul>  

As stated in the Vue Style Guide, self-closing components indicate that they are meant to have no content, similar to a deliberately blank page in a book. This approach helps keep your code cleaner by removing unnecessary closing tags.

While HTML doesn't allow custom elements to be self-closing, except for official "void" elements, Vue's template compiler can handle this strategy by ensuring compliance with DOM specifications during compilation.

Answer №2

Please refrain from using self-closing tags. Instead of <todo-item />, use

<todo-item></todo-item>

https://github.com/vuejs/vue/issues/1036

It appears that HTML5 components are not supported in this context.

Answer №3

Are you more inclined towards SFC or traditional HTML files? It appears that the issue lies in the HTML parser not allowing the use of <todo-item> within <ul>. You might want to consider using

<li :is="'todo-item'" v-for...>
instead, as it seems to work in your codepen setup.

<div id="app">
  <h1>TODOs</h1>
  <ul>
    <li :is="'todo-item'" v-for="(todo,index) in todos" v-bind:todo="todo" :key="index"></li>
    <li :key='"new_item"'>
      <input placeholder="What needs to be done?" type="text" v-model="new_todo" >
      <button v-on:click="add_todo()">Add</button>
    </li>
  </ul>  
  <p>You have finished {{ done }}/{{ total }}</p>
</div>

https://v2.vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats

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

Struggling with navigating the pop-up window in Webdriverio?

While conducting my testing with selenium and webdriverio, I encountered an issue when interacting with a pop-up PayPal window. Despite being able to switch to the pop-up successfully and confirming that a form element is enabled, I faced an error message ...

jQuery file uploader only transmitting a single chunk

Currently, I am integrating the jQuery file uploader into my Django application. I am encountering an issue where Django is only receiving one chunk of my large file. Initially, I suspected a problem with my UploadFileHandler; however, upon logging the ch ...

What are the steps to customize the appearance of a folder in the web browser?

Is it possible to customize the presentation of folder contents dragged into Chrome using CSS, HTML, JavaScript, or other methods? I've heard about the HTML5 file API but not sure if that's applicable in this scenario. I think it would be intere ...

Incorporate radio buttons to toggle the visibility of div elements within Vue 3

In my current Vue 3.0.5 project, I am trying to toggle between different <div> elements using radio buttons, allowing only one element to be shown at a time. Interestingly, I noticed that when I include the v-bind directive before the value attribute ...

CSS style takes precedence over inline JavaScript transitions occurring from a negative position

I am puzzled by a JavaScript fiddle I created which contains two small boxes inside a larger box. Upon clicking either of the buttons labeled "Run B" or "Run C", the corresponding box undergoes a CSS transition that is controlled by JavaScript. Below is t ...

Importing the .css file within a React component for dynamic styling

In my component, I am trying to dynamically load a .css file based on the result of an AJAX call in componentDidMount(). I have attempted adding a <link> element in the render return (which did not work), and also tried injecting the tag directly int ...

Step-by-step guide to dynamically generating v-models with VueJS

Recently, I've started delving into Vue and utilizing the latest build of VueJS 2. I have a dataset coming from the database that looks something like this: { count: 3, options: { 'red': 10, 'blue': 20, ...

At what point does the cleanup function in UseEffect activate the clearInterval() function?

In the process of creating a timer for a Tenzie game, there is an onClick function that changes the state of isTimerActive from false to true when a user clicks a button. The initial value of tenzie state is also set to false, but once the user completes t ...

The "begin" parameter of the Angular limitTo filter appears to be ineffective in Angular version 1.3

Currently, I am attempting to implement a pagination feature on an array of users using Angularjs 1.3. ng-repeat="user in users | filter: searchText | orderBy: 'lastName' | limitTo:pageSize:startPosition track by user.lanId" I specifically want ...

Is there a way to ensure that GIFs in jQuery Mobile always start from the beginning?

My cross-platform mobile app built with jQuery Mobile is a small quiz application. After each correct or wrong answer, I display a 3-second GIF. Currently, I have set up the code to show the GIF for 3 seconds before moving on to the next page: else if ($. ...

Knockout failed to display the template using the provided data

Having difficulty with JavaScript's knockout library. I am trying to create a simple forum. I have a JavaScript file with two AJAX requests, one for topics and the other for posts. I also have an HTML template. function dealModel() { var self = thi ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

put two elements next to each other in a single row

This specific section contains an image positioned at the top, followed by two other components (within <ItemsContainer/>) at the bottom which display as separate rows. I am trying to place these two items (a grid and a chart) side by side within the ...

The state initialization process is beginning with an [object Object] being passed into the setState function

Within the async function provided below, I am invoking stationData to verify that an array of objects is being passed into bartData (which currently consists of an empty array). Upon exploration, I have observed a response containing the anticipated array ...

Verify if the form has been successfully validated

Here is a simple form using Tag Helper: <form asp-area="Admin" asp-controller="Categories" asp-action="EditCategory" method="post" id="CategoryForm"> <div class="row"> ...

Issues with scrolling, styling in css, and Vue challenges

I'm a beginner in this topic, currently working with Vue and CSS. I've managed to create a responsive menu, but I'm facing difficulty in implementing scroll only for the drop-down menu. The issue I'm encountering is that when I scroll, ...

What is the reason for the failure of the jQuery code to disable the submit button in the following snippet?

I am working on a feature to disable the submit button in a form when the username, email, password fields are empty. When all of them are filled, I want to enable the submit button. However, the current code is not disabling the submit button as expected. ...

The Socket IO Client is receiving a different object than the one that was sent by the Server

My server side code is sending an object to the client side, but the object received by the client is completely different from what was sent. Let me elaborate: Code Server side code: //Setting up the example var response={}; response.models=[]; respo ...

Guide on positioning the camera to track the model in a third-person view

Currently, I am tackling a project in Three.js where I am in need of setting up a third-person camera that tracks a 3D model as it navigates through the scene. Despite having a basic framework with animations and controls, I am encountering difficulties in ...

What are the best practices for managing DOM manipulation with TypeScript instead of plain JavaScript?

I'm a beginner in Typescript and I want to incorporate the following JavaScript functionality into a Typescript file. http://jsfiddle.net/SgyEW/10/ $('.toggler').live('click', function() { var idname = ...