简体   繁体   English

SwiperJS w/ SyntaxError:无法在模块错误之外使用导入语句

[英]SwiperJS w/ SyntaxError: Cannot use import statement outside a module error

I can't seem to figure out what is causing this error, so I will go into more detail below on what I have done and if anyone can spot anything off, let me know.我似乎无法弄清楚是什么导致了这个错误,所以我将 go 在下面更详细地说明我所做的事情,如果有人能发现任何问题,请告诉我。


Here is the error that I am getting :这是我得到的错误

Uncaught SyntaxError: Cannot use import statement outside a module未捕获的语法错误:无法在模块外使用 import 语句


Here is everything that I have done :这是我所做的一切

Here is the package.json :这是package.json

{
  "scripts": {
    "imagemin": "imagemin src/images/* -p --out-dir=dist/images",
    "start": "gulp style & gulp scripts"
  },
  "devDependencies": {
    "bulma": "^0.9.3",
    "gulp": "^4.0.2",
    "gulp-concat": "^2.6.1",
    "gulp-sass": "^5.1.0",
    "gulp-uglify": "^3.0.2",
    "gulp-uncomment": "^0.3.0",
    "imagemin-cli": "^7.0.0",
    "node-sass": "^7.0.1",
    "rough-notation": "^0.5.1",
    "sass": "^1.47.0",
    "swiper": "^7.4.1",
    "three": "^0.122.0",
    "vanta": "^0.5.21"
  }
}

Here is me enqueuing the node_modules swiperjs:这是我排队node_modules swiperjs:

wp_register_script(
    'Swiper',
    get_template_directory_uri() . '/node_modules/swiper/swiper-bundle.min.js',
    null, null, true
);
wp_enqueue_script('Swiper');

I switched over the script to support the type="module" on SwiperJS:我切换了脚本以支持 SwiperJS 上的type="module"

在此处输入图像描述

Here is my swiper.js file that gets compacted into main.min.js :这是我的swiper.js文件,它被压缩到main.min.js

document.addEventListener('DOMContentLoaded', function() {

    import Swiper, { Navigation, Pagination } from 'swiper';
    import 'swiper/css';
    import 'swiper/css/navigation';
    import 'swiper/css/pagination';

    Swiper.use([Navigation, Pagination]);

    new Swiper(".mySwiper", {
        direction: "vertical",
        slidesPerView: 1,
        spaceBetween: 30,
        mousewheel: true,
        pagination: {
            el: ".swiper-pagination",
            clickable: true,
        },
    });
});

Here is the final output which is enqueued and minified:这是入队和缩小的最终 output:

document.addEventListener("DOMContentLoaded",()=>{const e=Array.prototype.slice.call(document.querySelectorAll(".navbar-burger"),0);0<e.length&&e.forEach(o=>{o.addEventListener("click",()=>{var e=o.dataset.target;const t=document.getElementById(e);o.classList.toggle("is-active"),t.classList.toggle("is-active")})})}),document.addEventListener("DOMContentLoaded",function(){import e,{Navigation as t,Pagination as o}from"swiper";import"swiper/css";import"swiper/css/navigation";import"swiper/css/pagination";e.use([t,o]),new e(".mySwiper",{direction:"vertical",slidesPerView:1,spaceBetween:30,mousewheel:!0,pagination:{el:".swiper-pagination",clickable:!0}})}),document.addEventListener("DOMContentLoaded",function(){VANTA.BIRDS({el:"#main-hero",mouseControls:!0,touchControls:!0,gyroControls:!1,minHeight:200,minWidth:200,scale:1,scaleMobile:1,colorMode:"lerpGradient",color1:2829099,quantity:3,backgroundColor:16251129,birdSize:1.5})});

Does anyone know what might be happening?有谁知道可能会发生什么?

I believe that you don't need to import separate parts of the Swiper if you are using the bundled build of Swiper.如果您使用的是 Swiper 的捆绑版本,我相信您不需要导入 Swiper 的单独部分。 So, just try the same code without import statements and Swiper.use statement.因此,只需尝试不使用import语句和Swiper.use语句的相同代码即可。

Also, don't forget that in the default initialization, it might be helpful to enqueue the Swiper style file to have a default appearance.另外,不要忘记在默认初始化中,将 Swiper 样式文件排入队列以具有默认外观可能会有所帮助。

If you want to use only the required parts of Swiper, you should include the source file in your source files, not just enqueueing the already built file that is located in the Generatepress.如果您只想使用 Swiper 所需的部分,您应该在源文件中包含源文件,而不仅仅是将位于 Generatepress 中的已构建文件排入队列。

暂无
暂无

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

相关问题 Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module JavaScript 导入错误 - 未捕获的 SyntaxError:无法在模块外使用导入语句 - JavaScript import error - Uncaught SyntaxError: Cannot use import statement outside a module 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 未捕获的语法错误:无法在模块外使用导入语句 - Uncaught SyntaxError:Cannot use import statement outside a module Jest - SyntaxError: 无法在模块外使用 import 语句 - Jest - SyntaxError: Cannot use import statement outside a module SyntaxError:无法在模块外部使用导入语句(来自依赖项) - SyntaxError: Cannot use import statement outside a module (from dependency) 如何解决“未捕获的语法错误:无法在模块外使用导入语句”? - How to resolve “Uncaught SyntaxError: Cannot use import statement outside a module”? NodeJS --&gt; SyntaxError: 不能在模块外使用 import 语句 - NodeJS --> SyntaxError: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM