reactjs Swiper JS导入CSS在Next 13上不工作

u3r8eeie  于 2023-04-20  发布在  React
关注(0)|答案(2)|浏览(149)

我有这个组件,我从Swiper JS导入所需的和模块到组件。由于某种原因,文件无法导入或根本无法工作。这只发生在使用Next 13时。是否有类似技巧或解决方案的东西?

  1. "use client";
  2. import { Swiper, SwiperSlide } from "swiper/react";
  3. // Import Swiper styles
  4. import "swiper/css";
  5. import "swiper/css/pagination";
  6. // import required modules
  7. import { Pagination } from "swiper";
  8. export default function Home() {
  9. return (
  10. <Swiper
  11. pagination={{
  12. dynamicBullets: true,
  13. }}
  14. modules={[Pagination]}
  15. className="mySwiper"
  16. >
  17. <SwiperSlide>Slide 1</SwiperSlide>
  18. <SwiperSlide>Slide 2</SwiperSlide>
  19. <SwiperSlide>Slide 3</SwiperSlide>
  20. <SwiperSlide>Slide 4</SwiperSlide>
  21. <SwiperSlide>Slide 5</SwiperSlide>
  22. <SwiperSlide>Slide 6</SwiperSlide>
  23. <SwiperSlide>Slide 7</SwiperSlide>
  24. <SwiperSlide>Slide 8</SwiperSlide>
  25. <SwiperSlide>Slide 9</SwiperSlide>
  26. </Swiper>
  27. );
  28. }

Package.json:

  1. {
  2. "name": "swiper-app",
  3. "version": "0.1.0",
  4. "private": true,
  5. "scripts": {
  6. "dev": "next dev",
  7. "build": "next build",
  8. "start": "next start",
  9. "lint": "next lint"
  10. },
  11. "dependencies": {
  12. "@types/node": "18.15.11",
  13. "@types/react": "18.0.35",
  14. "@types/react-dom": "18.0.11",
  15. "autoprefixer": "10.4.14",
  16. "eslint": "8.38.0",
  17. "eslint-config-next": "13.3.0",
  18. "next": "13.2.4",
  19. "postcss": "8.4.21",
  20. "react": "18.2.0",
  21. "react-dom": "18.2.0",
  22. "swiper": "9.1.1",
  23. "tailwindcss": "3.3.1",
  24. "typescript": "5.0.4"
  25. }
  26. }
0dxa2lsx

0dxa2lsx1#

方法一:

尝试使用CDN。

不使用实验/app目录:

在***pages文件夹中创建一个名为_document.js***的文件,然后添加swiperjs css CDN。

  1. // _document.js
  2. import { Html, Head, Main, NextScript } from 'next/document'
  3. export default function Document() {
  4. return (
  5. <Html>
  6. <Head>
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css" />
  8. </Head>
  9. <body>
  10. <Main />
  11. <NextScript />
  12. </body>
  13. </Html>
  14. )
  15. }

使用实验/app目录:

编辑***app/layout.tsx***文件(注意:它可以是layout.js、layout.jsx、layout.ts、layout.tsx,具体取决于您的项目)

  1. // app/layout.tsx
  2. export default function RootLayout({
  3. // Layouts must accept a children prop.
  4. // This will be populated with nested layouts or pages
  5. children,
  6. }: {
  7. children: React.ReactNode;
  8. }) {
  9. return (
  10. <html lang="en">
  11. <head>
  12. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css" />
  13. </head>
  14. <body>{children}</body>
  15. </html>
  16. );
  17. }

方法二:

尝试在Next.js项目中启用***es-modules-support***。
要做到这一点:编辑***next.config.js***文件。并放入以下内容:

  1. // next.config.js
  2. module.exports = {
  3. // Prefer loading of ES Modules over CommonJS
  4. experimental: { esmExternals: true },
  5. // other configuration here...
  6. };
展开查看全部
2w3rbyxf

2w3rbyxf2#

尝试导入“swiper/swiper. min. css”而不是“swiper/css”;这对于应用程序结构很好地工作。

相关问题