我有这个组件,我从Swiper JS导入所需的和模块到组件。由于某种原因,文件无法导入或根本无法工作。这只发生在使用Next 13时。是否有类似技巧或解决方案的东西?
"use client";
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";
// import required modules
import { Pagination } from "swiper";
export default function Home() {
return (
<Swiper
pagination={{
dynamicBullets: true,
}}
modules={[Pagination]}
className="mySwiper"
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
<SwiperSlide>Slide 5</SwiperSlide>
<SwiperSlide>Slide 6</SwiperSlide>
<SwiperSlide>Slide 7</SwiperSlide>
<SwiperSlide>Slide 8</SwiperSlide>
<SwiperSlide>Slide 9</SwiperSlide>
</Swiper>
);
}
Package.json:
{
"name": "swiper-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@types/node": "18.15.11",
"@types/react": "18.0.35",
"@types/react-dom": "18.0.11",
"autoprefixer": "10.4.14",
"eslint": "8.38.0",
"eslint-config-next": "13.3.0",
"next": "13.2.4",
"postcss": "8.4.21",
"react": "18.2.0",
"react-dom": "18.2.0",
"swiper": "9.1.1",
"tailwindcss": "3.3.1",
"typescript": "5.0.4"
}
}
2条答案
按热度按时间0dxa2lsx1#
方法一:
尝试使用CDN。
不使用实验
/app
目录:在***
pages
文件夹中创建一个名为_document.js
***的文件,然后添加swiperjs css CDN。使用实验
/app
目录:编辑***
app/layout.tsx
***文件(注意:它可以是layout.js、layout.jsx、layout.ts、layout.tsx,具体取决于您的项目)方法二:
尝试在Next.js项目中启用***
es-modules-support
***。要做到这一点:编辑***
next.config.js
***文件。并放入以下内容:2w3rbyxf2#
尝试导入“swiper/swiper. min. css”而不是“swiper/css”;这对于应用程序结构很好地工作。