• nextjs引入tailwindcss 、antd、sass


    nextjs 之旅


    前言

    上一篇介绍了nextjs 的配置,这章主要是引入tailwindcssantd.


    一、Why tailwindcss?

    1. 原子化css 是最近十分火的技术,还有最小的windicss 和unocss,公司最近用的技术也是这个,我个人使用俩个月的评价是,在你有一定css使用基础情况下确实能够提高开发效率。
    2. 这个项目就用这个练练手吧,vue项目再用windicss吧。
    3. 争论虽然也有,但优缺点是看项目的。

    二、引入步骤

    1. tailwindcss

    https://www.nextjs.cn/docs/advanced-features/customizing-postcss-config
    next 提供了postcss 开箱即用

    1. 官网教程:https://www.tailwindcss.cn/docs/guides/nextjs
    2. 安装依赖:npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
    3. 创建配置文件:npx tailwindcss init -p
      会创建tailwind.config.jspostcss.config.js
      在tailwind.config.js 里面加入
      purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
    4. 创建styles/tailwind.css
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    • 1
    • 2
    • 3
    1. pages/_app.tsx引入styles/tailwind.css
    import "../styles/globals.css";
    import "../styles/tailwind./*  */css";
    import type { AppProps } from "next/app";
    
    function MyApp({ Component, pageProps }: AppProps) {
      return <Component {...pageProps} />;
    }
    
    export default MyApp;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 验证是否成功
      pages/index.tsx:
    import type { NextPage } from "next";
    import Head from "next/head";
    
    const Home: NextPage = () => {
      return (
        <div className="">
          <Head>
            <title>Create Next App</title>
            <meta name="description" content="Generated by create next app" />
            <link rel="icon" href="/favicon.ico" />
          </Head>
    
          <main>
            <div className="flex justify-center text-[red]"> tailwindcss</div>
          </main>
        </div>
      );
    };
    
    export default Home;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述
    ok! 大功告成。

    2. scss

    官方:
    https://www.nextjs.cn/docs/basic-features/built-in-css-support

    1. npm install sass
    2. 需要配置sass 的话在next.config.jssassOptions(略)
    3. 使用:
      pages/helloWorld/index.module.scss
    .helloWorld {
        .scsstest {
            color: red;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. pages/helloWorld/index.tsx
    import styles from "./index.module.scss";
    function About() {
      return (
        <div className={styles.helloWorld}>
          <div className={styles.scsstest}>helloWord</div>
        </div>
      );
    }
    
    export default About;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 验证,成功!

    3. antd

    1. 安装依赖(npm)

    yarn add next-plugin-antd-less
    yarn add --dev babel-plugin-import

    1. 新建.babelrc.js
    module.exports = {
      presets: [['next/babel']],
      plugins: [['import', { libraryName: 'antd', style: true }]],
    };
    
    • 1
    • 2
    • 3
    • 4
    1. next.config.js:
    /** @type {import('next').NextConfig} */
    const withAntdLess = require("next-plugin-antd-less");
    const nextConfig = {
      reactStrictMode: true,
      swcMinify: true,
    };
    
    module.exports = withAntdLess(nextConfig);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 使用:
      pages/index.tsx:
    import type { NextPage } from "next";
    import Head from "next/head";
    import { Button } from "antd";
    const Home: NextPage = () => {
      return (
        <div className="">
          <Head>
            <title>Create Next App</title>
            <meta name="description" content="Generated by create next app" />
            <link rel="icon" href="/favicon.ico" />
          </Head>
    
          <main>
            <div className="flex justify-center text-[red]">
              tailwindcss
              <Button type="primary">test Antd</Button>
            </div>
          </main>
        </div>
      );
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    1. 验证成功

    在这里插入图片描述


    总结

    引入了这三个开发配置。
    下一篇开发博客:)

  • 相关阅读:
    掌握Pillow:Python图像处理的艺术
    C++对象模型(8)-- 数据语义学:this指针
    关于使用API接口获取商品数据的那些事(使用API接口获取商品数据的步骤和注意事项。)
    【C++从0到王者】第三十一站:map与set
    合约广告平台架构演进实践
    javaee之黑马乐优商城3
    【Python】sqlmodel: Python 数据库管理ORM 的终极形态?
    ElasticSearch ( 五 ) ik中文分词
    31.springboot中的注解总结(spring,springmvc,springboot,mybatis,dubbo)
    opencv图像的本质
  • 原文地址:https://blog.csdn.net/weixin_44002250/article/details/126454432