简体   繁体   English

当我在 VueJS 中使用 Fetch 调用数据时,jquery 无法正常工作?

[英]jquery doesn't work correctly when I call data with Fetch in VueJS?

Owlcarousel-slider works fine when I use below code.当我使用下面的代码时,Owlcarousel-slider 工作正常。

HTML Code: HTML 代码:

<div class="banner">
          <div class="main-banner owl-carousel" id="homeage-data">
            <div class="item" v-for="slider in sliders">
              <div v-bind:class="slider.class_type"> <img v-bind:src="slider.image_src" alt="Electrro">
                <div class="banner-detail">
                  <div class="container">
                    <div class="row">
                      <div class="col-md-3 col-3" v-if="slider.align_right"></div>
                      <div class="col-md-9 col-8">
                        <div class="banner-detail-inner"> 
                          <span class="slogan">{{ slider.label }}</span>
                          <h1 class="banner-title" v-html="slider.banner_title"></h1>
                          <span class="offer">{{ slider.banner_desc }}</span>
                        </div>
                        <a class="btn btn-color big" v-bind:href="slider.button_link" v-if="slider.button_np" target="_blank">{{ slider.button_text }}</a>
                        <a class="btn btn-color big" v-bind:href="slider.button_link" v-else-if="!slider.button_np">{{ slider.button_text }}</a>
                      </div>
                      <div class="col-md-3 col-4" v-if="!slider.align_right"></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

JavaScript Code: JavaScript 代码:

<script>
    new Vue({
        el: '#homeage-data',
        data: {
            sliders: [
                {
                    "label": "Discover",
                    "banner_title": "Top Branded for <br>headphone",
                    "banner_desc": "best selling, and popular.",
                    "image_src": "images/banner1.jpg",
                    "button_text": "Shop Now!",
                    "button_link": "#link",
                    "class_type" : "banner-1",
                    "button_np": true,
                    "align_right": false
                },
                {
                    "label": "curved tv",
                    "banner_title": "Get latest TV models",
                    "banner_desc": "Get the top brands for TV",
                    "image_src": "images/banner2.jpg",
                    "button_text": "Shop Now!",
                    "button_link": "#link",
                    "class_type" : "banner-2",
                    "button_np": false,
                    "align_right": true
                },
                {
                    "label": "Premium",
                    "banner_title": "drone cameras",
                    "banner_desc": "The latest camera up to 30% off",
                    "image_src": "images/banner3.jpg",
                    "button_text": "Shop Now!",
                    "button_link": "#link",
                    "class_type" : "banner-3",
                    "button_np": true,
                    "align_right": false
                }
            ]
        },
        
        });
</script>

Output Image: (Very Nice ) Output 图片:(非常好) 非常好

But when I call the code remotely with Fetch, the image is broken.但是当我用 Fetch 远程调用代码时,图像被破坏了。

JavaScript Code: (I uploaded the slider array to the api.php file and called it remotely.) JavaScript 代码:(我将 slider 数组上传到 api.ZE1BFD762321E409CEE4AC0B6E4 文件并远程调用它)

<script>
    new Vue({
        el: '#homeage-data',
        data: {
            sliders: [
            ]
        },
        created() {
            fetch("/api/api.php")
                .then((response) => { return response.json() })
                .then((response) => { 
                    this.sliders = response.sliders;
                }); 
            }
        });
</script>

Output Image: Output 图片:

在此处输入图像描述

As I understand it, it cannot render JQuery code because it was added later.据我了解,它无法呈现 JQuery 代码,因为它是后来添加的。 No matter what I did, I couldn't find the right way.无论我做什么,我都找不到正确的方法。

I have not tried the code below.我还没有尝试过下面的代码。 It didn't happen:(它没有发生:(

<script>
    new Vue({
        el: '#homeage-data',
        data: {
            sliders: [
            ]
        },
        created() {
            fetch("/api/api.php")
                .then((response) => { return response.json() })
                .then((response) => { 
                    this.sliders = response.sliders;  
                });
                $(".main-banner, #client").owlCarousel({
                    //navigation : true,  Show next and prev buttons
                    items: 1,
                    nav: true,
                    slideSpeed : 300,
                    paginationSpeed : 400,
                    loop:true,
                    autoPlay: false,
                    dots: true,
                    singleItem:true,
                    nav:true
                });
            }
        });
</script>

Demo of using VueOwlCarousel in Vue2, without npm :在 Vue2 中使用 VueOwlCarousel 的演示,没有npm

 Vue.use(VueOwlCarousel); new Vue({ el: '#app', data: () => ({ slides: 0, options: { autoplay: true, autoplaySpeed: 800, autoplayTimeout: 1800, lazyLoad: true, autoplayHoverPause: true } }), mounted() { setTimeout(() => { this.slides = 7 }, 2000) }, })
 div.owl-carousel { width: 400px; height: 180px; }.owl-stage-outer { height: 111px; }.loader { height: 180px; display: flex; align-items: center; width: 400px; justify-content: center; } h2 { color: #ddd; } body { height: 100vh; display: flex; align-items: center; justify-content: center; margin: 0; }
 <script src="https://v2.vuejs.org/js/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue-owl-carousel@2.0.3/dist/vue-owl-carousel.min.js"></script> <div id="app"> <vue-owl-carousel:items="1" v-if="slides" v-bind="options"> <img v-for="s in slides":key="s":src="`https://placeimg.com/400/111/any?${s}`"> </vue-owl-carousel> <div v-else class="loader"> <h2>Loading...</h2> </div> </div>


Personal advice: do not mix jQuery with Vue.个人建议:不要将 jQuery 与 Vue 混用。 You only want one source of truth for your DOM manipulations.对于 DOM 操作,您只需要一个事实来源。

That's the underlying cause of your issue.这就是你的问题的根本原因。


Note: Owl carousel can't be initialised with empty slides.注意:猫头鹰轮播不能用空幻灯片初始化。 For this reason, I've placed a v-if="slides" (or, if slides would have been an array, v-if="slides.length" ).出于这个原因,我放置了一个v-if="slides" (或者,如果slides是一个数组, v-if="slides.length" )。
Also note Owl takes control over the carousel DOM element and, from that moment on, even if you change the slides array, it won't react to the change.另请注意,Owl 控制了轮播 DOM 元素,从那一刻起,即使您更改了 slides 数组,它也不会对更改做出反应。 If you wanted to update the slides, you'd need the owl instance, change its internal slides (which are built from the original DOM) and then call its internal .refresh() method.如果要更新幻灯片,则需要 owl 实例,更改其内部幻灯片(从原始 DOM 构建),然后调用其内部.refresh()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM