简体   繁体   English

如何在 javascript 中定义'swiper' object?

[英]How to define 'swiper' object in javascript?

I'm trying to display a horizontal timeline on my HTML page.我试图在我的 HTML 页面上显示水平时间线。 I've found this template online, where the timeline can be scrolled.我在网上找到了这个模板,可以在其中滚动时间线。 However, when I try to open the page, I'm receiving the following error "ReferenceError: Swiper not defined" in console, and unable to scroll the timeline.但是,当我尝试打开页面时,我在控制台中收到以下错误“ReferenceError: Swiper not defined”,并且无法滚动时间线。 How do I fix this?我该如何解决? Below is my code.下面是我的代码。

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.0"></script>
<script>
    const data = [{
            dateLabel: 'January 2017',
            title: 'Gathering Information'
        },
        {
            dateLabel: 'February 2017',
            title: 'Planning'
        },
        {
            dateLabel: 'March 2017',
            title: 'Design'
        },
        {
            dateLabel: 'April 2017',
            title: 'Content Writing and Assembly'
        },
        {
            dateLabel: 'May 2017',
            title: 'Coding'
        },
        {
            dateLabel: 'June 2017',
            title: 'Testing, Review & Launch'
        },
        {
            dateLabel: 'July 2017',
            title: 'Maintenance'
        }
    ];

    new Vue({
        el: '#app',
        data: {
            steps: data,
        },
        mounted() {
            var swiper = new Swiper('.swiper-container', {
                slidesPerView: 4,
                paginationClickable: true,
                grabCursor: true,
                paginationClickable: true,
                nextButton: '.next-slide',
                prevButton: '.prev-slide',
            });
        }
    })
</script>

The error here这里的错误

"ReferenceError: [Insert anything here] not defined" “ReferenceError:[在此处插入任何内容] 未定义”

Means that whatever you're trying to reference hasn't been defined and the browser engine does not recognize it as a native object意味着尚未定义您尝试引用的任何内容,并且浏览器引擎无法将其识别为本机 object

Swiper is not something Chrome or Firefox are born with Swiper 不是 Chrome 或 Firefox 与生俱来的

Try removing the VueJS CDN link on the top of your page and you'll get the same error for vue, so....尝试删除页面顶部的 VueJS CDN 链接,你会得到同样的 vue 错误,所以....

Include the Swiper library like you did with Vue像使用 Vue 一样包含 Swiper 库

<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/js/swiper.min.js" integrity="sha256-4sETKhh3aSyi6NRiA+qunPaTawqSMDQca/xLWu27Hg4=" crossorigin="anonymous"></script>

Here's where Swiper variable is defined if you're curious如果您很好奇,这里就是定义 Swiper 变量的地方

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

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