Vue's v-for directive is resulting in surprising extra elements being generated

Currently, I'm in the process of utilizing Nuxt Content to build a blog section for my website. After setting up the directory structure and testing some posts, I proceeded to create a query that fetches the posts and stores them as a ref for page usage. However, I've encountered an issue where the components displayed on the page do not correspond with what is retrieved by the Nuxt Content query.

Snippet

<script setup> 
const { data: recentBlogPosts } = await useAsyncData(`content-${path}`, () => {
  return queryContent()
    .only(['title', 'excerpt', 'created', 'updated', 'slug', 'tags', '_id'])
    .limit(5)
    .sort({ 'updated': -1 })
    .find()
})

index.vue Page Snippet

<nuxt-link v-for="post in recentBlogPosts" :key="post._id" :to="`blog/${post.slug}`">
  <BlogPostShort class="blog-post-short"
    :title="post.title"
    :createdDate="post.created"
    :updatedDate="post.updated"
    :excerpt="post.excerpt"
    :tags="post.tags" />
</nuxt-link>

The output shown on the webpage

Vue DevTools displaying only 5 components generated by the v-for directive

I attempted adding a :key="post._id" to no avail. My assumption was providing Nuxt a way to connect a post to the backend data might assist, but unfortunately, it didn't work out.

EDIT Here is an image of the recentBlogPosts variable introduced to the page using mustache-syntax (cannot inline it due to being a low-rep newbie):

https://i.sstatic.net/PBAJM.png

Answer №1

The issue I encountered was related to the use of a v-for directive with a <NuxtLink> element. After making some adjustments, the updated code now functions correctly:

<div v-for="post in recentBlogPosts" :key="post._id">
  <NuxtLink :to="`blog/${post.slug}`">
    <BlogPostShort class="blog-post-short"
      :title="post.title"
      :createdDate="post.created"
      :updatedDate="post.updated"
      :excerpt="post.excerpt"
      :tags="post.tags" />
  </NuxtLink>
</div>

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

How can a Vue component be created with a template that solely consists of a property's text?

Looking for a way to incorporate plain text within a Vue component template area? The current method involves wrapping the text in a div, but this causes layout issues when concatenating components with other text fragments. This results in awkward spacing ...

Apple on High Sierra page demonstrated an innovative use of grid parallax

I am attempting to create a unique parallax effect within a two-sided grid design. Apple's High Sierra page features an interesting parallax effect. ( ) Take a look at this image showcasing the parallax effect in action In this setup, the right sid ...

An endless expanse of flooring within a THREE.JS environment

What is the most effective method for creating a floor that extends infinitely in all directions in my canvas three.js scene? Would it be more efficient to link a THREE.PlaneGeometry to the camera's position so that it moves along with the camera? A ...

Using the Twit package on the client side: a step-by-step guide

I have been utilizing the Twit package for searching tweets. After executing the js file in the console, I am able to see the tweets but when trying to use them on my webpage, an error 'Uncaught ReferenceError: require is not defined' pops up. Th ...

Tips for transferring form data to a popup form by clicking a link in MVC

I'm currently working on an MVC project where I have successfully created a table. Everything is functioning properly, but now I need to implement a feature where clicking on a link in a specific row opens a popup form displaying the data from that ro ...

Obtain data from function using an XMLhttprequest

I am completely new to JavaScript and currently working on building a simple web page that performs the following tasks: Retrieves the IP address of a server Sends a GET request to that server Parses the response from the request Displays filtered data i ...

What is the best way to test a function that returns a JSX element in a unit test

I created a function that takes in a parameter and returns a JSX.Element //title.tsx export const getTitle = (pageTitle: string) => { return <title> {pageTitle} </title>; } This is how I am currently testing it: describe('Title c ...

Creating a Custom Alert Box in Javascript with Image Alignment

I have just finished creating a custom alert box using Javascript, complete with text and images. However, I am facing an issue with alignment. The output is not as expected. I am trying to display the correct mark and text on the same line. Can someone ...

How can the Vue-google-charts wrapper in Vue.js be used to generate a Google line chart?

What is the process for creating a line chart in Vue.js using the vue-google-charts wrapper? Can you provide guidance on adding rows and columns as data to the chart? Here is an example of how this can be done in Vanilla JS: var data = new google.visual ...

Transfer the values selected from checkboxes into an HTML input field

I am attempting to capture the values of checkboxes and then insert them into an input field once they have been checked. Using JavaScript, I have managed to show these values in a <span> tag successfully. However, when trying to do the same within a ...

Issue encountered when setting a background image in Angular

When trying to add a background image and set it to cover the entire browser window, I encountered an issue where the image appeared very small and did not fully cover the background. As someone new to Angular, my understanding of how this process works i ...

All Event Monitor

Is it possible to use Event Listeners in jQuery to display information when a specific word is clicked? For example, showing a definition when a word is clicked. Thanks, Adam. I need help with creating a feature where clicking on a person's name in a ...

Discovering the final step of a for loop when working with JavaScript objects

Currently, my data consists of: {12: Object, 13: Object, 14: Object} I need help figuring out how to detect the final iteration in the for loop below: for (var i in objs){ console.log(objs[i]); } Does anyone have any solutions? ...

Dealing with numerous promises nested within a single promise

I'm in the process of developing a service method that fetches data from an API and returns it as a promise. The challenge here is that I need to ensure that the parent object, along with all its child objects, are fully retrieved before resolving the ...

Issue: ng-disabled directive is not functioning properly on buttons in Foundation for Apps.In

I recently encountered an issue with a Foundation for Apps button not working properly with the ng-disabled directive in a form. Here is the HTML code: <form name="loginForm" novalidate> <div class="grid-bloc ...

[Protractor][Internet Explorer 11]-->I am facing an issue where clicking on a link does not open a new tab even though I have configured the IE browser settings properly

For example: ele.click(); is able to open a new tab in Chrome and Firefox, but not in IE11. However, if I manually click the link, it will open a new tab in IE11. Can someone explain why this is happening? What steps should I take to resolve it? Thank y ...

What is the best way to determine if an element retrieved through jQuery matches an element stored in an array?

Currently, I am trying to store elements in an array so that I can later identify which one was clicked on. However, my current method is failing to work as expected. I plan to use the array index for some calculations. The if statement in the code block ...

Retrieving video information using Dailymotion API with JSON and jQuery

I have been struggling to understand the issue even after consulting the Dailymotion API and various sources. I am attempting to retrieve data from Dailymotion for a specific video ID by using the following code: $.getJSON('https://api.dailymotion.co ...

Is it possible to load textures in Three.js and Cordova without relying on a server or HTTP?

I am currently working on developing a Cordova game using Three.js with the goal of making it playable offline. However, I have encountered an issue where Three.js requires texture files to be served via HTTP, making it challenging to achieve offline funct ...

Unsure of how to create a record of calculations made on the calculator

I am struggling with creating a calculator that includes a history feature. I have the basic functioning of the calculator working, but now I want to modify it so that it displays a history of operations performed by the user. The goal is for the history t ...