微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在具有动态数据的javascript中创建多图片轮播?

如何解决如何在具有动态数据的javascript中创建多图片轮播?

我正在尝试创建一个多图像轮播,其中从API中提取了图像。使用循环在dom中渲染图像。然后尝试通过该类访问图像并存储在“ slides”数组中。该API需要花费一些时间,因此“ slides”数组未初始化。

import { LightningElement,track } from 'lwc';

export default classCarousel extends LightningElement
{
    @track firstIndex = 1;
    @track lastIndex = 7;
    @track slides;
    @track Results;

    connectedCallback() {
        //EXTERNAL API CALLED FROM HERE
        this.ExternalApi();
    }

    showSlides(f,l) {
        console.log("show")

        this.slides = this.template.querySelectorAll('.nds-package-thumb'); // TRYING TO INITIALIZE SLIDES ARRAY BY ACCESSING THE CLASS OF THE RENDERED IMAGES,HERE IS THE ISSUE,THIS GETS INITIALIZED FirsT AND ARRAY GETS RENDERED LATER
        console.log(this.slides.length)
        console.log(this.slides)
        console.log(f,l)
        let i;
        let j;
        let k;


        if (f < 1) {

            for (i = 0; i < 7; i++) {
                this.slides[i].style.display = "inline-block";
            }

        }
        if (l < this.slides.length + 1) {
            console.log("called else")
            for (i = f - 1; i < l; i++) {
                this.slides[i].style.display = "inline-block";
                console.log("first loop")

            }
            for (j = l; j < slides.length; j++) {
                this.slides[j].style.display = "none";
                console.log("second loop")

            }
            if (f > 1 && f < 7) {
                for (k = 0; k < f - 1; k++) {
                    this.slides[k].style.display = "none";
                    console.log("third loop")
                }
            }
        }
    }

    externalApi() {

        const params = {
            input: JSON.stringify({ "Category": "Signature" }),sClassName: 'Controller',sMethodName: 'getContentByCategory',options: '{}'
        };
        this._actionClass
            .executeAction(params,null,this,null)
            .then(response => {
                let parsedResponse = JSON.parse(response.result.CategoryDetailList);
                this.Results = parsedResponse;           //IMAGES GETTING RENDERED IN HTML FROM RESULTS ARRAY
                console.log(parsedResponse);


                this.showSlides(this.firstIndex,this.lastIndex); //SHOW FUNCTION CALLED  
            })
            .catch(error => {
                console.log(error);
            });
    }
}

试图应用异步等待,但没有解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。