You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Next.js应用中Swiper Grid模块无法正常实现预期效果

无法在Next.js中实现Swiper Grid功能(官方示例也失效)

我最近两天一直在尝试在我的Next.js应用中实现Swiper Grid功能,但所有尝试都失败了:

  • 试过Stack Overflow上的多种解决方案,网格效果始终不生效
  • 直接复制Swiper官方提供的可运行代码示例到新建的Next.js页面,仍然无法达到预期的网格布局效果

以下是官方可运行的示例代码(我直接复制到项目中):

import React, { useRef, useState } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/grid";
import "swiper/css/pagination";
import { Grid, Pagination } from "swiper";

export default function App() {
  return (
    <>
      <Swiper
        slidesPerView={3}
        grid={{ rows: 2, }}
        spaceBetween={30}
        pagination={{ clickable: true, }}
        modules={[Grid, 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>
    </>
  );
}

我的项目情况:

  • 使用Next.js框架
  • 预期效果:实现2行3列的网格滑动布局
  • 实际效果:网格布局不生效,幻灯片仍为单列排列或无网格结构

针对性解决方案建议

1. 解决Next.js客户端渲染限制(核心问题)

如果你的项目用的是Next.js 13+的App Router,默认组件是服务器组件,而Swiper依赖浏览器环境,必须强制转为客户端组件:

'use client'; // 必须添加这一行,放在所有导入之前
import React from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/grid";
import "swiper/css/pagination";
import { Grid, Pagination } from "swiper";

export default function SwiperGridPage() {
  return (
    <>
      <Swiper
        slidesPerView={3}
        grid={{ rows: 2 }}
        spaceBetween={30}
        pagination={{ clickable: true }}
        modules={[Grid, Pagination]}
        className="mySwiper"
        style={{ height: '400px' }} // 给容器明确高度,避免布局塌陷
      >
        <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>
    </>
  );
}

2. 修复版本兼容性问题

确保swiper和swiper/react版本完全一致,版本不匹配会导致模块失效,执行以下命令重新安装:

npm uninstall swiper swiper/react
npm install swiper@latest

(Swiper v9+对Next.js的兼容性更好,推荐使用最新稳定版)

3. 避免样式被框架清除

如果你用Tailwind CSS等样式框架,需要在配置文件中添加Swiper路径,防止样式被PurgeCSS移除:

// tailwind.config.js
module.exports = {
  content: [
    './app/**/*.{js,ts,jsx,tsx,mdx}',
    './pages/**/*.{js,ts,jsx,tsx,mdx}',
    './components/**/*.{js,ts,jsx,tsx,mdx}',
    // 添加这一行
    './node_modules/swiper/**/*.js',
  ],
  // 其他配置...
}

4. 解决SSR/SSG渲染冲突

如果使用静态生成或服务器渲染,尝试用动态导入并禁用SSR:

import dynamic from 'next/dynamic';

const Swiper = dynamic(() => import('swiper/react').then(mod => mod.Swiper), { ssr: false });
const SwiperSlide = dynamic(() => import('swiper/react').then(mod => mod.SwiperSlide), { ssr: false });

// 组件内正常使用即可

内容的提问来源于stack exchange,提问作者Siddiqui Noor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:34:05