Utilize nested components within a slot to enhance functionality, as I am unsure of another way to phrase it

Currently, I am utilizing a slider component known as Hooper. It's all functioning smoothly - of course, I've created a separate component for it because I prefer not to have it included in my global app at all times.

<template>
    <hooper :infiniteScroll="true">
        <slide v-for="(image, index) in images" :style=" { backgroundImage: 'url(\'img/' + image + '\')' }" :key="index">dsadasd</slide>
    </hooper>
</template>

<script>

    import { Hooper, Slide } from 'hooper';
    import 'hooper/dist/hooper.css';

    export default {
        props: [
            'images'
        ],
        name: 'Hooper',
        components: {
            'hooper': Hooper,
            'slide': Slide
        }
    };
</script>

In order to use this component, I need to add it to my blade file like so:

@extends('layouts.app')

@section('content')

    <carousel :images="[ 'slider-1.jpg', 'slider-2.jpg', 'slider-3.jpg' ]" class="carousel"></carousel>

    <div class="container">
        <h1 class="p-4 text-lg font-sans">Home</h1>
    </div>

@endsection

A Dilemma

My aim is to be able to insert any HTML content into each slide and also specify the image name. Ideally, within my blade file, it would look like this:

@extends('layouts.app')

@section('content')

    <carousel class="carousel">
        <slide :style=" { backgroundImage: 'url(\'img/image-1.jpg\')' }">
            <h3>header text</h3>        
            <p>dsadasfhshshsgfgas</p>
        </slide>
        <slide :style=" { backgroundImage: 'url(\'img/image-2.jpg\')' }">
            <h3>header text 2</h3>        
            <p>dsadasdsdsadsafhshshsgfgas</p>
        </slide>
    </carousel>

    <div class="container">
        <h1 class="p-4 text-lg font-sans">Home</h1>
    </div>

@endsection

Unfortunately, slide is not recognized as a registered component. How can I address this issue?


An Alternate Approach

I had an idea where I could create a slot for each loop within my component and then override that slot in the blade file, like this:

Carousel.vue

<template>
    <hooper :infiniteScroll="true" class="carousel">
        <slide v-for="(image, index) in images" :style=" { backgroundImage: 'url(\'img/' + image + '\')' }" :key="index">
            <slot :name="'slide' + index"></slot>
        </slide>
    </hooper>
</template>

<script>

    import { Hooper, Slide } from 'hooper';
    import 'hooper/dist/hooper.css';

    export default {
        props: [
            'images'
        ],
        name: 'Hooper',
        components: {
            'hooper': Hooper,
            'slide': Slide
        }
    };
</script>

home.blade.php

<carousel :images="[ 'slider-1.jpg', 'slider-2.jpg', 'slider-3.jpg' ]" class="carousel">
    <template v-slot="slide1">
        <h2>dsdasdadas</h2>
        <p>dsdadasdasds</p>
    </template>
</carousel>

However, this method doesn't seem to be working as expected. Any suggestions on how to fix this? Thanks!

Answer №1

The dynamic slot names concept should function correctly in your previous example, but there is a misunderstanding in the usage of v-slot. It seems like you intended to use it as follows:

<carousel :images="[ 'slider-1.jpg', 'slider-2.jpg', 'slider-3.jpg' ]" class="carousel">
  <template #slide1="slotProps">    // slotProps are optional here, if you want to pass data from Hooper/Slide to the slot
    <h2> Header </h2>
    <p> Text </p>
  </template> 
  <template #slide2>
    <h3> Different Header </h3>
    <div> Different Text </div>
  </template>
</carousel>

Using v-slot="slide1" is essentially the same as v-slot:default="slide1". It means that you are assigning the props received by the default slot (which is not available) to the name slide1.

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

Having difficulty retrieving information from a factory in AngularJS

I'm encountering an issue with my db factory where the data returned from the database is coming back as 'undefined' after successfully posting it to the client. This is how my factory function is structured: uno.factory('adbFactory&a ...

I seem to be encountering a problem with utilizing SpotLights and shadows within Three.js

In my simple scene, there is a single .dae mesh and a 7000*7000 plane underneath it. I want to illuminate the scene with a high SpotLight to cast a shadow of the mesh on the ground. However, there seems to be an issue! No matter how high I position the Spo ...

Set the height of the div to match the length of the downward swipe

I am working on gradually revealing a div as the user swipes down on the body element. Currently, I am using jQuery along with hammer.js to detect the swipe gesture, but I need assistance in determining the distance of the swipe and adjusting the height o ...

When triggered by an event, a different function is unable to access the Angular scope variable as it is

Service angular.module('tested-user') .service('tests', function($http, $q){ this.getTests = $q.when($http.get('http://localhost:3000/tests.json'));}); Controller tests.getTests.then(function(data){ noOfTests = ...

Creating views solely with HTML, JavaScript, and AJAX, without the need for JSP or any similar technologies

I've been exploring the idea of creating an MVC architecture using only javascript, html, and AJAX (potentially jQuery or a similar library) to generate views, instead of relying on JSP, Velocity, or Freemarker in Java applications. This concept could ...

Error: The function pathRegexp is not defined

While attempting to conduct tests on my project with jest, I encountered an error code that seems unrelated to the actual testing process. It appears to be more of a dependency or Node Express compatibility issue. `● Test suite failed to run TypeError: ...

Tips for refreshing the current Angular 2 Component

I'm looking for a way to refresh the same component in Angular 2. Can anyone provide guidance? Below is the code snippet I have: import { Component, OnInit, ElementRef, Renderer } from '@angular/core'; import { Router, ActivatedRoute, Para ...

How to Turn Off Depth Testing in Three.js

I am currently using an EffectComposer in my project: renderer = new THREE.WebGLRenderer(); renderer.setDepthTest(false); ... composer = new THREE.EffectComposer( renderer); However, I noticed that when I attempt to disable the depth test with the follow ...

Saving data for editing on a Laravel page can be achieved by leveraging the powerful features

I have implemented my create page using vue.js. I called the vue.js file using a component. Now, for the edit page, I followed the same procedure by calling the vue component in the blade. However, I am unsure how to save data for the edit page. Can anyone ...

I am seeing the object in req.body displayed in reverse order within my Node.js and Express backend application

I encountered an issue while developing a To-do list web application using the MERN stack with Redux. The problem arises when I try to send post data to the backend. In my form, the textfield area is named 'task' (e.g., ) and I input 'jonas& ...

The Nextjs framework is experiencing a high total blocking time in its *****.js chunk

As I analyze the performance of next.js in my project, I am noticing a high total blocking time. Specifically, the "framework" chunk consistently takes more than 50ms. It seems that this chunk corresponds to the react and react-dom JavaScript files. Does ...

Understanding how to display a component within another component in Vue.js

I am faced with a scenario where I have a component that has the following template: <div v-for:"item in store" v-bind:key="item.type"> <a>{{item.type}}</a> </div> Additionally, I have another component named 'StoreCompone ...

Tips for activating a click event on a changing element within a Vue.js application

I am working on creating dynamically generated tabs with a specific range of time (from 8am to 9am). My goal is to automatically trigger a click event when the current time falls within this range. However, I am facing an issue where the ref is being ident ...

Is wp_create_nonce necessary for enhancing security in WordPress ajax requests?

As I work on developing a plugin, I am considering the security implications of utilizing ajax requests. If I opt to call ajax via a direct php file URL instead of using Wordpress' admin-ajax.php, do I still need to implement wp_create_nonce for added ...

What is the reason behind suggesting to avoid using $scope.attribute and instead favor $scope.model.attribute in AngularJS?

While studying the ng-book, there is a section that recommends encapsulating attributes within another attribute when using the $scope, as shown below: $scope.model.attribute instead of $scope.attribute The author points out that this practice is benefic ...

Connecting with a php anchor and hash symbol in a website URL

Looking to include a PHP anchor along with a hash anchor in an HTML link. <?php echo '<a href="test.php?page='.$i.'#hash">'.$i.'</a>'; ?> The PHP code successfully echoes the link, but upon clicking it, th ...

What is the process for customizing the default animation in the Dialog component?

Currently I am utilizing the Material UI framework and looking into modifying the default animation for a Dialog opening. Specifically, I would like the Dialog to appear from bottom to top when it opens up. Any suggestions on how this can be achieved? Ap ...

Issues encountered with JavaScript when trying to use the jQuery API

I'm trying to fetch random quotes using a Mashape API, but when I click the button, nothing happens. I've included the JS and HTML code below. Can anyone spot an issue with the JS code? The quote is not displaying in the div. Thank you! $(' ...

The AJAX request encountered an error while trying to send a POST request to the http://localhost/upload/undefined endpoint. The

I've been scouring the internet and testing for hours, but I'm still unable to identify the source of the error. I could really use some assistance here. I meticulously followed a tutorial on multiple file uploads with drag & drop functionali ...

The click event listener only seems to fire on the second attempt

This block of code is designed to function as a search algorithm that also provides keyword suggestions. When a user clicks on a keyword, it is supposed to be placed into an input textbox. The possible keywords are "first" and "second". However, there is ...