Encountering a problem while attempting to display an id dynamically using Vue.js?

AppTabs.vue

<template>
  <div
    :class="{
      'flex space-x-4': variant === 'horizontal',
    }"
  >
    <ul
      class="
        list-none
        bg-opacity-30
        p-1.5
        rounded-lg
        text-center
        overflow-auto
        whitespace-nowrap
      "
      :class="{
        'flex items-center mb-6': variant === 'vertical',
      }"
    >
      <li
        v-for="(tab, index) in tabList"
        :key="index"
        class="w-full px-4 py-1.5 rounded-lg tabs"
        :class="{
          'text-blue-600': index + 1 === activeTab,
          'text-black': index + 1 !== activeTab,
        }"
      >
        <label
          :for="`${_uid}${index}`"
          v-text="tab.tab"
          class="cursor-pointer block"
        />
        <input
          :id="`${_uid}${index}`"
          type="radio"
          :name="`${_uid}-tab`"
          :value="index + 1"
          v-model="activeTab"
          class="hidden"
        />
      </li>
    </ul>

    <template v-for="(tab, index) in tabList">
      <div
        :key="index"
        v-if="index + 1 === activeTab"
        class="flex-grow rounded-lg p-4 tab-content tabs-cont-ht"
      >
        <slot :name="index + 1" />
      </div>
    </template>
  </div>
</template>

App.vue

export const datalist = [
  {
    id: 1, tab: "Tab 1a", val: "1", content: "Content 1"
  },
  { id: 2, tab: "Tab 2", val: "2", content: "Content 2" },
  { id: 3, tab: "Tab 3", val: "3", content: "Content 3" },
  { id: 4, tab: "Tab 4", val: "4", content: "Content 4" },

];

How can I modify the code above to use my own custom IDs instead of auto-generated ones for input fields? My API requires specific field names like DeptId, but when I tried integrating it, I encountered errors due to the automatically generated IDs. Can you please advise on the changes needed to remove the auto-generation of IDs and use my own provided IDs from the API?

I have tested removing the ID generation and binding data from an array but faced multiple errors. Please guide me on how to successfully replace the autogenerated IDs with my specified IDs like DeptId.

Answer №1

It is important to note that the first defining slot should have a name, not an id, for a non-default slot in your Vue components.

<slot :name="`tabPanel-${index + 1}`" />

To override these slots, you can either use v-slot="slot-name" or the shorthand # notation. Make sure to update your code accordingly like so:

<template v-for="content in contentList" :v-slot="content.id">

In Vue3, you can also use v-slot:[dynamicSlotName]. Keep in mind that the slot name you create may not be equal to the id you use in your parent component. This is because the slot name will be overridden when using v-slot with the name of the slot.

For more detailed information, it is recommended to refer to the slots documentation on the Vue website:

Vue2: https://v2.vuejs.org/v2/guide/components-slots.html

Vue3: https://vuejs.org/guide/components/slots.html

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

What are the Javascript Keycodes for the letter 'a' - is it 65 or

I am currently working with JavaScript on my MacBook Pro running OSX 10.11.x, using the Chrome browser. The function I am using is: window.onkeypress = function(e) { var key = e.keyCode ? e.keyCode : e.which; console.log("keypressed = " + key); } ...

Increasing values in Mongoose using $inc can be done by following these steps

I've been struggling to increment a field value using $inc in my code. My schema looks like this: var postSchema = mongoose.Schema({ title : { type: String, required: true }, body : { type: String, default: '' }, coun ...

The bootstrap down button refuses to function despite having all the necessary files integrated

My bootstrap drop down button isn't functioning correctly even after ensuring all necessary javascript and popper libraries are included as per npm download. <!DOCTYPE html> <html lang="en"> <head> <title>three grid </title ...

Is it possible to use jQuery's .attr method to change the href attribute if a certain text is contained within a DIV

(Twitter) I'm struggling with how to incorporate the .attr method at the start of links/A/href when the selected element contains specific words: $(".tweet:contains('government','Anonymous')").each(function(){ $old_url = $(thi ...

Is the npm mqtt module continuously running but not performing any tasks?

I'm relatively new to the world of JS, node.js, and npm. I am attempting to incorporate a mqtt broker into a project for one of my classes. To gain a better understanding of how it functions, I installed the mqtt module from npm. However, when I tried ...

How to Trigger a Child Component Function from a Parent Component in React.js

I want to trigger a function in my child component when a button is clicked in the parent component. Parent Component: class Parent extends Component{ constructor(props){ super(props); this.state = { //.. } } ...

The Redux store is duplicating the state across different reducers, causing it to appear twice

Having Trouble Viewing Different States for Two Reducers in Redux DevTools In my React project, I am attempting to incorporate a second reducer using Redux to gain a better understanding of the overall state. However, when I inspect the state in Redux Dev ...

Whenever I select a link on a navigation bar, it transports me to the desired section of the page. However, I often find that the navbar ends up

Recently, I came across some website templates where clicking on a link in the navbar smoothly scrolls to the corresponding section with perfect alignment. The content at the top of the page aligns perfectly with the top of each division. Upon attempting ...

Using a JavaScript function inside a loop without the need for a click event

I have successfully created a slideshow, but I am facing an issue with looping it continuously. Currently, I have implemented a click event to restart the script, but I want it to loop without the need for any interaction. window.CP.exitedLoop(0);functio ...

Is it possible to implement a route within a controller in Express.js?

In my controller, I currently have the following code: module.exports.validateToken = (req, res, next) => { const token = req.cookies.jwt; //console.log(token); if (!token) { return res.sendStatus(403); } try { const ...

Variability in data sizes within Chart.js

I created a sample chart in MS Excel where each data point varies in size according to the legend provided. Can Chart.js accommodate this kind of customization? https://i.sstatic.net/5qjNy.png ...

How can I avoid transpiling in TypeScript when setting ESNext as the target in tsconfig.json?

I came across a similar question that I thought would be helpful: How to maintain ES6 syntax when transpiling with Typescript, but unfortunately, it didn't solve my issue... It's slightly different. When running yarn tsc --project tsconfig.json ...

Exploring the world of JSON and JavaScript data structures

Can someone provide some clarification please? var a = '{"item":"earth", "color":"blue", "weight":920}'; Is the data type of a a string or an array ? var b = JSON.parse(a); What is the data type of b - an object or an array ? ...

There seems to be a discrepancy with the IP to hex conversion equation in my JavaScript code. The result I'm getting doesn't align with what other

How to check if an item is a valid IP address (e.g. 254.253.242.222)? `var h0 = Math.pow(256,0);` `var h1 = Math.pow(256,1);` `var h2 = Math.pow(256,2);` var h3 = Math.pow(256,3); var splitup = item.split('.'); var iHex = ...

Using Async functions with Node.js Express.js router

I've been trying to search on Google, but I can't seem to find a clear answer to this one... Is it possible to pass ES7 async functions to the Express router? For example: var express = require('express'); var app = express(); app.ge ...

Using Three.js to ensure that child object3d expands within the boundaries of its parent object

In this scenario, I have 2 Object3Ds with one nested inside the other: var obj1 = new THREE.Object3D(); var obj2 = new THREE.Object3D(); obj1.add(obj2); Assuming obj1 is AxB units wide, how can I instruct obj2 to match that size? Essentially, how can I m ...

What are the appropriate times to utilize functional components in Vue.js?

After learning about functional components in Vue.js 2 (Documentation), I discovered that they offer significant performance improvements: Functional components, being just functions, are much more efficient to render. It appears that when emitting even ...

Issue between Promise and EventEmitter causing race conditions in ExpressJS

Currently, I am working on a NodeJS/Express web application where users can upload files that are then parsed using the connect-busboy module and saved to a database with Sequelize. Once the data is stored, I aim to redirect the user to a specific page. Ho ...

Combining a form and table on a single webpage, I am curious how to utilize the form to update the table effectively using Express and Node.js

Seeking guidance in website development as I face a particular challenge. I aim to create a dynamic search page for a website. The form should allow users to select a location or category, with the same page updating with relevant results. Despite succes ...

"Experience a blank page equipped with vue-router featuring history mode and optimized for production purposes

I am a beginner with Vue and encountered the following error. I created a small project using the template from https://github.com/vuejs-templates/webpack with its default settings. After adding vue-router, everything works well in development mode. Howev ...