如何在next

  }
]

]
}


重启应用:重新启动你的Next.js应用,可以看到Ant Design库已经成功集成到项目中。


通过以上步骤,你就可以在Next.js项目中成功集成Ant Design库并使用它的组件了。


## 官方建议操作方式:


### 目录结构:


![](https://img-blog.csdnimg.cn/direct/d185c903608c4abab59d8d6292e2d864.png)


### 主要文件内容设置:


package.json



{
“private”: true,
“scripts”: {
“dev”: “next”,
“build”: “next build”,
“start”: “next start”
},
“dependencies”: {
“@ant-design/cssinjs”: “^1.16.1”,
“@ant-design/icons”: “^5.1.4”,
“antd”: “^5.0.0”,
“next”: “latest”,
“react”: “18.2.0”,
“react-dom”: “18.2.0”
},
“devDependencies”: {
“@types/node”: “^20.4.5”,
“@types/react”: “^18.2.17”,
“@types/react-dom”: “^18.2.7”,
“typescript”: “^5.1.6”
},
“browser”: {
“fs”: false,
“path”: false
}
}


AntRegistry.tsx



‘use client’

import React from ‘react’
import { useServerInsertedHTML } from ‘next/navigation’
import { StyleProvider, createCache, extractStyle } from ‘@ant-design/cssinjs’
import type Entity from ‘@ant-design/cssinjs/es/Cache’

interface StyledRegistryProps {
children: React.ReactNode
}

const StyledComponentsRegistry = ({ children }: StyledRegistryProps) => {
const cache = React.useMemo(() => createCache(), [])
useServerInsertedHTML(() => (
<style
id=“antd”
dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }}
/>
))
return {children}
}

export default StyledComponentsRegistry


 layout.tsx



import React from ‘react’
import type { Metadata } from ‘next’
import { Inter } from ‘next/font/google’
import StyledComponentsRegistry from ‘./AntdRegistry’
import ‘./globals.css’

const inter = Inter({ subsets: [‘latin’] })

export const metadata: Metadata = {
title: ‘Create Next App’,
description: ‘Generated by create next app’,
}

interface RootLayoutProps {
children: React.ReactNode
}

function RootLayout({ children }: RootLayoutProps) {
return (


{children}


)
}

export default RootLayout


page.tsx



‘use client’

import React from ‘react’
import Link from ‘next/link’
import { SmileFilled } from ‘@ant-design/icons’
import {
Button,
DatePicker,
Form,
InputNumber,
Select,
Slider,
Switch,
ConfigProvider,
} from ‘antd’
import theme from ‘./themeConfig’

const HomePage = () => (

<div style={{ padding: 100, height: ‘100vh’ }}>



<SmileFilled style={{ fontSize: 48 }} />

Welcome to the world !




<Form
layout=“horizontal”
size={‘large’}
labelCol={{ span: 8 }}
wrapperCol={{ span: 8 }}
>
<Form.Item label=“Input Number”>
<InputNumber
min={1}
max={10}
style={{ width: 100 }}
defaultValue={3}
name=“inputNumber”
/>
</Form.Item>
<Form.Item label=“Switch”>

</Form.Item>
<Form.Item label=“Slider”>

</Form.Item>
<Form.Item label=“Select”>
<Select
defaultValue=“lucy”
style={{ width: 192 }}
options={[
{ value: ‘jack’, label: ‘Jack’ },
{ value: ‘lucy’, label: ‘Lucy’ },
{ value: ‘Yiminghe’, label: ‘yiminghe’ },
{ value: ‘lijianan’, label: ‘lijianan’ },
{ value: ‘disabled’, label: ‘Disabled’, disabled: true },
]}
/>
</Form.Item>
<Form.Item label=“DatePicker”>

</Form.Item>
<Form.Item style={{ marginTop: 48 }} wrapperCol={{ offset: 8 }}>

OK

<Button style={{ marginLeft: 8 }}>Cancel
</Form.Item>




)

export default HomePage


themeConfig.ts



import type { ThemeConfig } from ‘antd’

const theme: ThemeConfig = {
token: {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值