Create a named scopedSlot dynamically

Looking to incorporate the following template into my component's render function, but struggling with how to do so.

This is the current template structure:

<template>
  <div>
      <slot name="item" v-for="item in filteredItems" :item="item">
        {{ item.title }}
      </slot>
  </div>
</template>

Below is the code snippet of my component:

export default {
  props: {
    items: {
      type: Array,
      required: true,
    },
    search: {
      type: String,
      default: ""
    }
  },
  methods: {
    filterByTitle(item) {
      if (!("title" in item)) { return false; }
      return item.title.includes(this.search);
    }
  },
  computed: {
    filteredItems() {
      if (this.search.length === "") {
        return this.items;
      }
      return this.items.filter(this.filterByTitle);
    }
  },
  render: function(h) {
    // Seeking guidance on transferring the template content here
    return h('div', ...);
  }
};

I sincerely appreciate any assistance provided.

Answer №1

If you want to implement scoped slots, utilize $scopedSlots. Find more details here.

Check out this sample code:

...
  render(h) {
    return h(
      'div',
      this.filteredItems.map(item => {
        let slot = this.$scopedSlots[item.title]
        return slot ? slot(item) : item.title
      })
    )
  }
...

JSFiddle

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

Vue: Exploring the Restriction of Using refs in Component Templates

I can't seem to figure out Vue refs. Whenever I define them in my main Vue instance's template, they work perfectly fine. However, when I define them within a component template, they don't seem to work at all. What could be causing this iss ...

Vue.js event handlers

I am in the process of creating a basic app that will include a few listeners. However, I am struggling to determine the best approach for organizing the logic behind it. Here is an example of the HTML: <div id="playerProgressBar"> <div id=" ...

Conditionally render a component only if a calculated property is not activated

When the state preference in my vuex store changes, I need to update the DOM by calling the checkValue method each time. This is how my index.html looks like: <div id="app"> <my-component></my-component> <my-other-component&g ...

Combining VeeValidate and BootstrapVue to create an interactive editable table row

I'm facing an issue with creating editable rows on a bootstrap b-table and veevalidate. Is it possible to have multiple ValidationObservers and then validate them by calling a single method? <b-table :items="items"> <template v-slot:cell ...

Ways to dynamically fetch data by merging the response outcome with a dynamic parameter from the route in Vue.js

For the first time, I have been tasked with dynamically retrieving object parameters from the URL parameter. I am aware that I can use this.$route.params.[somelink-parameter] to obtain the URL parameter, and I understand how to retrieve and store the respo ...

Guide to creating flexible routes with multiple changing parameters in Vue 3 using Vue Router

What is the best way to implement dynamic routes with multiple dynamic parameters in any order using Vue 3 and Vue Router? These parameters should be able to be passed in any order. In our web application, we have over 500 views which makes it impractic ...

Is it possible for pdfjs-dist to be used with Typescript?

Is there a way to preview a PDF as a canvas without importing pdfjs-dist into my project? I have already used the command $yarn add pdfjs-dist to install pdfjs-dist. Do I need to include any additional imports? import pdfjsLib from "pdfjs-dist/build ...

Having trouble with my TinyMCE Editor not loading content data in the Edit.vue component of my Vue 3 project with Vite

I am currently working on a Vue 3 project using Vite and incorporating Editor.vue components with TinyMCE. The code snippet for my Editor.vue component is shown below: My Editor.vue code: <template> <div class="mb-6"> < ...

Waiting for all API queries in VueJS is important to ensure that all data has been

Hey there, I currently have an API set up using Django Rest Framework and the front end is built with VueJS. I have a form view that can either be used to Add New or Modify existing data. The structure of the form remains the same, but it checks if an ID ...

execute the command "npm run build" but exclude "dotnet run"

Trying to streamline my workflow here. I have a setup with ASP.NET framework and a VueJS-Project nested inside it. My goal is to run "npm install" and "npm run build" within the VueJS folder using only the "dotnet run" command to start the ASP.NET applica ...

Loading dynamic components asynchronously in Vue 3 allows for improved performance and enhanced user experience

My goal is to dynamically display components based on their type. Here's how I'm approaching it: I have several similar components that should show different content depending on their type. Using the defineAsyncComponent method, I can easily im ...

Vue 3 Single Page Application. When selecting, it emits the language and the contentStore does not update the content exclusively on mobile devices

My Vue 3 Single Page Application is built on Vite 4.2 and TypeScript 5.02. When I click to select a language, it emits lang.value and in the parent component App.vue, contentStore should update the content. It works flawlessly on my Linux Ubuntu desktop i ...

Vue - Struggling to send props to dynamically imported Web Components

Long time no see, old friend. I am navigating to a Vue file that is asynchronously imported (via import) upon routing. The template for this file is shown below, mainly serving as a way to load in a heavier web component and create separation from that co ...

An alternative way to update the radio button's checkability status using the :checked or :unchecked attributes

Within the code snippet below, there are two radio buttons present. Initially, both of them are unchecked. When the first radio button is checked, the log message in change_() function prints "male". On the other hand, when the second radio button is check ...

The integration of Quill.js is problematic within a Vuetify v-dialog component

As I attempt to integrate a Quill.js editor within a Vuetify v-dialog, I'm encountering an issue where the toolbar dropdowns fail to close when the user clicks outside of the currently open dropdown. You can view my implementation on JSFiddle: https: ...

Is it possible to import Vue directly from the "/path/to/vue.js" file without using npm or NodeJs?

Is it possible to build a web app using just a single index.js file and importing other available files like shown in this image: https://i.stack.imgur.com/02aFF.png encountering the error message: import not found: default Do you have to use Vuejs wit ...

Organizing a Vue.js SPA project: Implementing Vuex store and API calls efficiently

Here is how I have organized the structure of my Vue app: components/ article/ AppList.vue common/ AppObserver.vue NoSSR.vue layout/ AppFooter.vue AppHeader.vue ui/ AppButton. ...

Unable to manipulate or customize the V-slot component

I recently implemented the v-flip feature on my Nuxt webpage, resulting in the following template: <div class="about__cards"> <vue-flip class="about__single-card" active-click width = "350px" height="450px"> <template v-slot:front> ...

Issue: Module 'C:viteinvite.js' not found while setting up vue.js in Laravel project

Decided to dive into learning Laravel and Vue.js, so I followed a tutorial for installation found here: https://youtu.be/WLQDpY7lOLg?t=1058 Everything was going smoothly until I reached the npm run dev command... Below are the commands I executed in the ...

Utilizing Chart.js with Vue for dynamic time axis plots from JSON data through computed properties

Trying to generate a chart with a time axis in Vue using Chart.js has been a challenge. Initially, everything worked perfectly when the data was set directly in the Data object as shown below: chartData: { datasets: [ { backgroundC ...