Displaying data in a Vue list using key-value pairs

Can the key value be displayed in the component when passing it in the list?

The data is structured like this:

      { id: 1,  title: "test", text: "Lorem ipsum doloret imes", date: "20-01-2020" },
      { id: 2,  title: "tes", text: "Lorem ipsum doloret imes", date: "20-01-2020" },
      { id: 3,  title: "est", text: "Lorem ipsum doloret imes", date: "20-01-2020" },

I want to show the id in the child component:

 <single-news-template v-for="m in data"
                v-bind:key="m.id"
                v-bind:title="m.title"
                v-bind:text="m.text"
                v-bind:date="m.date">
                </single-news-template>

This is the component code:

<v-list-item dense
                >

            <v-list-item-avatar>
                    <v-avatar color="indigo" size="36">
            <span class="white--text headline">{{key}}</span>
            </v-avatar>
                </v-list-item-avatar>
                <v-list-item-content>
                    <v-list-item-title><a href="">{{title}}</a></v-list-item-title>
                    <v-list-item-subtitle>[...]{{text}}[...]</v-list-item-subtitle>
                    <v-list-item-subtitle>{{date}}</v-list-item-subtitle>
                </v-list-item-content>
    </v-list-item>



<script>
    export default {
        name: "SingleNewsTemplate",
        props: ["key","title", "text", "date"]
    }
</script>

However, the key does not appear in the avatar item.

Answer №1

To avoid conflicts, consider renaming it to something else as the key is already reserved (e.g., id)

<single-news-template v-for="m in data"
  v-bind:key="m.id"
  v-bind:id="m.id"
  v-bind:title="m.title"
  v-bind:text="m.text"
  v-bind:date="m.date">
</single-news-template>

Alternatively, you can utilize $vnode.key.

<span class="white--text headline">{{$vnode.key}}</span>

Answer №2

The reason it's not functioning is because the key attribute is reserved for Vue itself. To find out more, check out https://v2.vuejs.org/v2/api/#key or https://v2.vuejs.org/v2/guide/list.html#Maintaining-State.

Instead of utilizing :key, try using a different property name such as :id.

<single-news-template v-for="m in data"
  :key="m.id"
  :id="m.id"
  :title="m.title"
  :text="m.text"
  :date="m.date"></single-news-template>

Within your component, make sure to reference the property id rather than key.

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

JQuery, Draggable delete

I created a shopping cart with drag-and-drop functionality for nodes. http://jsfiddle.net/dkonline/Tw46Y/ Currently, once an item is dropped into the bucket (slot), it cannot be removed. I'm looking to add that feature where items can be removed from ...

Is it possible for a single form input in MySQL/Laravel/Vue to submit multiple values to multiple columns simultaneously?

As a newcomer to backend development, I am facing a challenge that I haven't found an answer to yet. Essentially, I am wondering if it's possible to have a form with two "name" fields and two corresponding "value" fields. The goal is to allow the ...

What is the best way to create a mock URL in axios for unit testing purposes?

Scenario In this application, the URL is only accessible in production and cannot be accessed locally. During unit testing, it becomes necessary to mock the response from that URL. Solution Refer to this helpful tutorial for guidance. Current Implement ...

Sending a single data point via Ajax on the client side

I am facing an issue with connecting frontend and backend systems. I want to send a single value (radio1Value) from a JavaScript function using jQuery AJAX upon clicking. Is the function structured correctly as I have written it? If yes, how should the R ...

Error: React Native Component Exception - a potential hiccup in

As a newcomer to React Native, I've encountered an issue while trying to bind data from a local JSON server API. Everything worked fine when I used a class component for my EventList, but after integrating Navigation in App.js and changing it to a fun ...

Converting data types when transferring a JavaScript variable to a PHP variable

I have a boolean value stored in a JavaScript variable var value=true; alert(typeof(value)); //Output: boolean I need to pass this variable to a PHP file through AJAX $.ajax({ type: 'POST', data: {value:value}, url: 'ajax.php& ...

Tips for creating a static background when displaying a modal popup in AngularJS

Incorporating a modal popup to modify a row within a grid view has been my recent task. Leveraging the row.getProperty() function, I successfully extracted the row values within the modal. However, an inconvenience emerged when attempting to edit a value ...

using node.js to save query results as global variables

Help needed! I'm struggling to pass the results of my query statement to a global variable in order to dynamically configure my jsganntimproved chart. Any suggestions on what could be going wrong? In the snippet below, the console.log(taskItem) retur ...

My JavaScript seems to be having an issue with .className - can anyone help me troubleshoot

I'm currently working on a navigation menu, and my objective is to have the class change to .nav-link-active when a link is clicked, while also reverting the current .nav-link-active back to .nav-link. Here's the code snippet I am using: <he ...

Using jQuery to select the next table cell vertically

Is there a way to utilize jQuery to access the cell (td) directly below a given cell in a traditional grid-layout HTML table (where each cell spans only one row and column)? I understand that the code below will assign nextCell to the cell immediately to ...

The expiration time and date for Express Session are being inaccurately configured

I'm experiencing an issue with my express session configuration. I have set the maxAge to be 1 hour from the current time. app.use( session({ secret: 'ASecretValue', saveUninitialized: false, resave: false, cookie: { secure ...

Conceal a row in a table using knockout's style binding functionality

Is it possible to bind the display style of a table row using knockout.js with a viewmodel property? I need to utilize this binding in order to toggle the visibility of the table row based on other properties within my viewmodel. Here is an example of HTM ...

Encountering an 'undefined' response when passing an array in Express's res.render method

After my initial attempt at creating a basic node app, I discovered that the scraper module was working correctly as data was successfully printed in the command window. Additionally, by setting eventsArray in index.js, I confirmed that Express and Jade te ...

Tips for isolating all the Javascript loaded via ajax or jquery.load within a specific child scope instead of a global scope

I am currently working on a solution to embed a third-party page into my site using ajax/jquery.load() to avoid using iframes (CORS requirements are already handled by the third party). My dilemma lies in the fact that the main host site loads jquery 1.x ...

Utilizing React to create an infinite loop, where an onClick event triggers an image change that updates the source of

I'm experiencing an infinite loop in my React application. I'm attempting to include a previous Image and next Image button in the development stage using an image tag. However, when my component loads up, I encounter errors. Does anyone have a ...

What is the best way to patiently wait for lines to be printed out one by one

I am in the process of creating my own personal website with a terminal-style design, and I'm looking to showcase a welcome banner followed by a welcoming message. The effect I have in mind involves lines appearing individually from top to bottom and ...

Using the HTMLTextAreaElement object in Vue.js

I'm utilizing the Laravel package "laracasts/utilities" to transmit Laravel variables to JavaScript files. Below is the code snippet from my controller: JavaScript::put([ 'description' => $room->description ]); He ...

What is the best way to access event.target as an object in Angular 2?

Apologies for my limited English proficiency. . I am trying to write code that will call myFunction() when the user clicks anywhere except on an element with the class .do-not-click-here. Here is the code I have written: document.addEventListener(' ...

Creating a visual representation on a canvas using JQuery

$.fn.colorPicker = function () { const $div = this; const $colorPickerIcon = $("<img></img"); const $canvas = $("<canvas></canvas>").addClass("canvas"); const $context = $canvas.getContext("2d"); const $closeButton ...

"Stellar.js fails to function properly when applied to elements loaded dynamically through AJAX requests

I recently implemented the amazing Stellar.js for parallax effects in a project of mine. However, I've encountered an issue: Stellar.js does not recognize when I update content via AJAX (specifically loading new div elements from an HTML file and rep ...