创建react&ts&antd项目

1.按照 https://ant.design/docs/react/use-in-typescript

npx create-react-app antd-demo-ts --template typescript
cd antd-demo-ts
yarn start

  yarn add antd

2. 还原打包代码,这样可以修改打包出来的静态资源目录,和修改端口

npm run eject

3.修改 src/App.tsx

import React, { FC } from 'react';
import { Button } from 'antd';
import './App.css';

const App: FC = () => (
  <div className="App">
    <Button type="primary">Button</Button>
  </div>
);

export default App;

4.修改src/App.css

@import '~antd/dist/antd.css';

5.将css改成less,修改 config/webpack.config.js

将module的rules选项增加一个 test: /\.less$/i,这段

module: {
      strictExportPresence: true,
      rules: [
        // Handle node_modules packages that contain sourcemaps
        shouldUseSourceMap && {
          enforce: 'pre',
          exclude: /@babel(?:\/|\\{1,2})runtime/,
          test: /\.(js|mjs|jsx|ts|tsx|css)$/,
          loader: require.resolve('source-map-loader'),
        },
        ...
         {
              test: /\.less$/i,
              use: [
                {
                  loader: 'style-loader',
                },
                {
                  loader: 'css-loader',
                },
                {
                  loader: 'less-loader',
                  options: {
                    lessOptions: {
                      javascriptEnabled: true,
                      modifyVars: {
                        'root-entry-name': 'default',
                        'primary-color': '#6256c9',
                        'link-color': '#6256c9',
                        'border-radius-base': '6px',
                      },
                      javascriptEnabled: true,
                    },
                  },
                },
              ],
            },
    ...

          ],
        },
      ].filter(Boolean),
    },

6.增加less和less-loader

yarn add less less-loader

7.

/* src/App.ts */
- import './App.css';
+ import './App.less';
/* src/App.less */
- @import '~antd/dist/antd.css';
+ @import '~antd/dist/antd.less';

 

上一篇:antd 组件小知识点 之Modal


下一篇:【React】解决antd样式没有显示问题(网页频闪问题)