establishing a cycle to iterate through the newly formed component

I have a set of data that returns labels, and I need to use this data to create a component.

The component is already constructed when passing the values. Now, I aim to use a for loop to continuously add entries and generate components as required.

This is my attempt:

data() {
  return {
    options: [{
        heading: 'Welcome to the Card',
        subheading: 'Manage your Profile here'
      },
      {
        heading: 'Pay your bills',
        subheading: 'Manage your bills and payments here'
      }
    ]
  }
}

I am attempting to iterate through it like so:

<div v-for="(value, key) in options">
  <componentName {{key}} = {{value}}/>  
</div>

Previously, the above code looked like this:

<componentName :heading='Welcome to the Card' :subheading='Manage your Profile here'/>

While this method works fine, I need to recreate <componentName each time I want to add more entries, which I'd like to avoid. Instead, I want to maintain one entry and feed it an array of objects.

I'm using Vue2. What could be the issue with my approach?

Answer №1

You're almost there! Based on the data you provided, the template should look something like this:

<div v-for="(option, index) in options" :key="index">
  <h3>{{ option.heading }}</h3>
  <p>{{ option.subheading}}</p>
</div>

Alternatively, if you have a custom component that accepts props for heading and subheading:

<componentName
  v-for="(option, index) in options"
  :key="index"
  :heading="option.heading"
  :subheading="option.subheading"
/>

If you provide more of your code, I can create a runnable snippet for you to test out.

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

Splitting files with Webpack generates dynamic chunk files

Anticipating to only have two distinct chunks named vendors and commons, webpack unexpectedly generates a new chunk file for specific entries with a delimiter. optimization: { splitChunks: { chunks: 'all', cacheGroups: { ...

Dynamically importing files in Vue.js is an efficient way to

Here's the code snippet that is functioning correctly for me var Index = require('./theme/dir1/index.vue'); However, I would like to utilize it in this way instead, var path = './theme/'+variable+'/index.vue'; var Inde ...

What could be causing React onclick events to not trigger when wrapped within a Vue application? (No additional libraries)

As I dive into the world of combining React and Vue components, I encountered an interesting challenge... const RootTemplate = () => { return ( <div id="vue-app"> ... <IconButton color="inherit" onClick={thi ...

Ways to access information and functions from another component

Creating a timer dashboard where alarms can change the background color of the timer. Looking to display the timer on a different page with the set background color from the dashboard, but struggling to pass data between components successfully. http ...

Steps for launching a hyperlink in a browser on Vue Native

I'm having trouble finding information on how to properly use the Linking component in react-native. The resources available are limited, and I'm not sure if I am implementing it correctly... Below is what I have in my template: <nb-button :o ...

Exploring the discrepancies in utilizing the i18n library versus directly incorporating locale from JSON in vue.js

Recently, I decided to incorporate Chinese language into my Vue app. To achieve this, I loaded the entire JSON text into Vuex and utilized the stored text directly, instead of relying on an i18n library. I'm curious to know if there are any potential ...

Is Axios phasing out support for simultaneous requests?

According to the current axios documentation, there is a section that is not very well formatted: Concurrency (Deprecated) It is advised to use Promise.all instead of the functions below. These are helper functions for managing concurrent requests. axio ...