Next.js + Antd 踩坑记录

1,000 阅读1分钟

前言

在学React框架,记录一下在用Next.js+Antd实现前端过程中遇到的问题 使用版本:

  • "next": "14.2.4"
  • "antd": "^5.19.0"
  • "@types/node": "20.14.9"
  • "react": "18.3.1"

1.Next.js引入Antd时报错

报错描述

export default AccountBookFilled;

SyntaxError: Unexpected token 'export'

Screenshot 2024-07-06 at 01.15.14.png

解决方法

需要用transpilePackages来解决这个问题,next.config.js文件应该改成:

/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, transpilePackages: [ "antd", "@ant-design", "rc-util", "rc-pagination", "rc-picker", "rc-notification", "rc-tooltip", "rc-tree", "rc-table", ], }; module.exports = nextConfig;

相关链接

github.com/ant-design/… github.com/ant-design/… juejin.cn/post/731056…