Nuxt3 + Tailwind CSS初始化项目

Nuxt3项目初始化

项目初始化

安装

tex 复制代码
npx nuxi@latest init <project-name>

构建

  1. 创建src目录。

  2. nuxt.config.ts中配置srcDir

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
    })
  3. src目录下创建相关目录。

    tex 复制代码
    assets:资源目录(资源文件会被构建工具处理)
    components:组件目录
    layouts:布局目录
    middleware:中间件目录
    pages:页面目录
    plugins:插件目录
    public:静态资源目录(资源文件不会被构建工具处理)
    store:状态树目录
    app.vue:Nuxt应用程序入口文件
  4. 创建默认布局。

    src/layouts/default.vue

    vue 复制代码
    <template>
      <div>
        <header>Header</header>
        <slot></slot>
        <footer>Footer</footer>
      </div>
    </template>
    
    <script setup lang="ts"></script>
    
    <style scoped></style>
  5. 创建index页面。

    src/pages/index.vue

    vue 复制代码
    <template>
      <div>
        Nuxt App
      </div>
    </template>
    
    <script setup lang="ts">
    
    </script>
    
    <style scoped>
    
    </style>
  6. 修改app.vue

    vue 复制代码
    <template>
      <div>
        <!-- 布局组件 -->
        <NuxtLayout>
          <!-- 页面组件 -->
          <NuxtPage></NuxtPage>
        </NuxtLayout>
      </div>
    </template>
  7. 启动项目。

    tex 复制代码
    npm run dev

Tailwind CSS

安装

tex 复制代码
npm install -D tailwindcss postcss autoprefixer

构建

  1. 创建tailwind css配置文件。

    tex 复制代码
    npx tailwindcss init --ts
  2. tailwind css添加到postcss中。

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
    })
  3. 添加需要使用tailwind css的文件路径。

    tailwind.config.ts

    typescript 复制代码
    import type { Config } from "tailwindcss";
    
    export default {
      content: ["./src/**/*.{js,vue,ts}", "./src/app.vue", "./src/error.vue"],
      theme: {
        extend: {},
      },
      plugins: [],
    } satisfies Config;
  4. 使用tailwind css默认样式。

    src/assets/css/main.css

    css 复制代码
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
  5. 引入默认样式文件。

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        css: ["~/assets/css/main.css"],
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
    })
  6. 使用tailwind css

    src/pages/index.vue

    vue 复制代码
    <template>
      <div class="text-xl">Nuxt App</div>
    </template>
    
    <script setup lang="ts"></script>
    
    <style scoped></style>
  7. 启动项目。

    tex 复制代码
    npm run dev

VueUse

安装

tex 复制代码
npm i @vueuse/nuxt @vueuse/core

构建

  1. 添加@vueuse/nuxt模块。

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        css: ["~/assets/css/main.css"],
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
        modules: ["@vueuse/nuxt"],
    })
  2. 使用vueuse

    src/pages/index.vue

    vue 复制代码
    <template>
      <div>
        <h1>Nuxt App</h1>
        <p>X坐标:{{ x }}</p>
        <p>Y坐标:{{ y }}</p>
      </div>
    </template>
    
    <script setup lang="ts">
      import { useMouse } from "@vueuse/core";
    
      const { x, y } = useMouse();
    </script>
    
    <style scoped></style>
  3. 启动项目。

    tex 复制代码
    npm run dev

Pinia

安装

tex 复制代码
// pinia依赖
npm i pinia @pinia/nuxt
// pinia数据持久化依赖
npm i -D @pinia-plugin-persistedstate/nuxt

构建

  1. 添加@pinia/nuxt & @pinia-plugin-persistedstate/nuxt模块。

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        css: ["~/assets/css/main.css"],
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
        modules: ["@vueuse/nuxt", "@pinia/nuxt", "@pinia-plugin-persistedstate/nuxt"],
    })
  2. 创建状态树。

    src/store/count.ts

    typescript 复制代码
    import { defineStore } from "pinia";
    
    export const useCount = defineStore(
    	"count",
        () => {
            const number = ref(0);
            const double = computed(() => number.value * 2);
            const increase = () => {
                number.value++;
            };
            return { number, double, increase };
        },
        {
            // pinia数据持久化默认存储为cookie
            persist: true,
        }
    );
  3. 使用状态树。

    src/pages/index.vue

    vue 复制代码
    <template>
      <div>
        <h1>Nuxt App</h1>
        <p>number: {{ store.number }}</p>
        <p>double: {{ store.double }}</p>
        <button @click="store.increase()">increase</button>
      </div>
    </template>
    
    <script setup lang="ts">
      import { useCount } from "~/store/count";
    
      const store = useCount();
    </script>
    
    <style scoped></style>
  4. 启动项目。

    tex 复制代码
    npm run dev

状态持久化

Option Store

typescript 复制代码
export const useCount = defineStore('count', {
    state: () => ({
        number: 0,
    }),
    getters: {
        double() {
            return this.number * 2;
        }
    },
    actions: {
        increase() {
            this.number++;
        }
    },
    // 数据持久化
    persist: true
})

Setup Store

typescript 复制代码
import { defineStore } from "pinia";

export const useCount = defineStore(
	"count",
    () => {
        const number = ref(0);
        const double = computed(() => number.value * 2);
        const increase = () => {
            number.value++;
        };
        return { number, double, increase };
    },
    {
        // 数据持久化
        persist: true,
    }
);

默认配置

  • 数据存储于cookie
  • keystore.$id
  • 使用JSON.stringify()JSON.parse()进行序列化 / 反序列化
  • 整个store都会被保存

选项配置

  • key:设置数据保存的key
  • storage:设置存储方式,使用persistedState配置(cookielocalStoragesessionStorage
  • paths:用于指定需要保存的数据
  • serializer:指定序列化方式
  • debug:用于设置是否打印错误

Icon

安装

tex 复制代码
// 下载依赖同时将模块添加到nuxt.config.ts
npx nuxi module add icon

构建

  1. 设置icon前缀以及路径。

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        css: ["~/assets/css/main.css"],
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
        modules: ["@vueuse/nuxt", "@pinia/nuxt", "@pinia-plugin-persistedstate/nuxt", "@nuxt/icon"],
        icon: {
            customCollections: [
                // 通用icon
                {
                    prefix: "common",
                    dir: "./src/assets/icons/common",
                },
                // 黑暗模式icon
                {
                    prefix: "dark",
                    dir: "./src/assets/icons/dark",
                },
                // 明亮模式icon
                {
                    prefix: "light",
                    dir: "./src/assets/icons/light",
                },
            ],
      	},
    })

    ps:

    ​ 由于需要切换明亮 & 黑暗模式,icon会改变。

    ​ 但没有找到一个好的解决方案,替换icon颜色时填充部分也会添加上颜色。

    ​ 所以选择使用明亮 & 暗黑两套icon。

    ​ 如果阅读该文章的你有更好的解决方案,可以评论交流下,前端萌新来的。

  2. 使用icon图标。

    src/pages.index.vue

    vue 复制代码
    <template>
      <div>
        <h1>Nuxt App</h1>
        <div class="flex">
          <!-- name: 前缀:icon文件名 -->
          <Icon name="light:search" />
          <Icon name="light:cart" />
          <Icon name="light:profile" />
        </div>
      </div>
    </template>
    
    <script setup lang="ts"></script>
    
    <style scoped></style>
  3. 启动项目。

    tex 复制代码
    npm run dev

Color Mode

安装

tex 复制代码
npm install --save-dev @nuxtjs/color-mode

构建

  1. 添加@nuxtjs/color-mode模块并配置colorMode

    nuxt.config.ts

    typescript 复制代码
    export default defineNuxtConfig({
        devtools: { enabled: true },
        srcDir: "./src",
        css: ["~/assets/css/main.css"],
        postcss: {
            plugins: {
              tailwindcss: {},
              autoprefixer: {},
            },
      	},
        modules: [
            "@vueuse/nuxt", 
            "@pinia/nuxt", 
            "@pinia-plugin-persistedstate/nuxt", 
            "@nuxt/icon", 
            "@nuxtjs/color-mode"
        ],
        icon: {
            customCollections: [
                // 通用icon
                {
                    prefix: "common",
                    dir: "./src/assets/icons/common",
                },
                // 黑暗模式icon
                {
                    prefix: "dark",
                    dir: "./src/assets/icons/dark",
                },
                // 明亮模式icon
                {
                    prefix: "light",
                    dir: "./src/assets/icons/light",
                },
            ],
      	},
        colorMode: {
            preference: "light",	// 初始值
        	fallback: "light",		// 未找到系统默认值返回该值
        	classSuffix: "",		// 设置class前缀
        }
    })
  2. 切换模式。

    src/pages/index.vue

    vue 复制代码
    <template>
      <div>
        <h1>Nuxt App</h1>
        <p>当前主题模式:{{ mode.value }}</p>
        <button @click="changeMode()">切换主题模式</button>
      </div>
    </template>
    
    <script setup lang="ts">
    const mode = useColorMode();
    const changeMode = () => {
      mode.preference = mode.value === "light" ? "dark" : "light";
    };
    </script>
    
    <style scoped></style>
  3. 启动项目。

    tex 复制代码
    npm run dev

主题切换

Tailwind CSS & Color Mode实现主题切换。

  1. 设置主题css变量。

    src/assets/css/main.css

    css 复制代码
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    @layer base {
      /* light模式 */
      html {
        --primary-cover: #ffffff;
        --primary-title: #000000;
        --primary-content: #1a202c;
        --primary-subhead: rgba(26, 32, 44, 0.24);
      }
      
      /* dark模式 */
      html[class="dark"] {
        --primary-cover: #1e1e27;
        --primary-title: #ffffff;
        --primary-content: #f7fafc;
        --primary-subhead: rgba(247, 250, 252, 0.24);
      }
    }
  2. tailwind.config.ts中扩展colors

    typescript 复制代码
    import type { Config } from "tailwindcss";
    
    export default {
      content: ["./src/**/*.{js,vue,ts}", "./src/app.vue", "./src/error.vue"],
      theme: {
        extend: {
          colors: {
            title: "var(--primary-title)",
            content: "var(--primary-content)",
            subhead: "var(--primary-subhead)",
            cover: "var(--primary-cover)",
          },
        },
      },
      plugins: [],
      darkMode: "class",	// 以class进行模式切换
    } satisfies Config;
  3. 页面中使用主题颜色。

    src/pages/index.vue

    vue 复制代码
    <template>
      <div class="bg-cover">
        <h1 class="text-title">Nuxt App</h1>
        <p class="text-content">Content</p>
        <p class="text-subhead">Subhead</p>
        <div class="flex">
          <!-- 仅在客户端渲染 -->
          <ClientOnly>
            <Icon :name="`${mode.value}:search`" />
            <Icon :name="`${mode.value}:cart`" />
            <Icon :name="`${mode.value}:profile`" />
          </ClientOnly>
        </div>
        <p class="text-content">当前主题模式:{{ mode.value }}</p>
        <button class="text-content" @click="changeMode()">切换主题模式</button>
      </div>
    </template>
    
    <script setup lang="ts">
    const mode = useColorMode();
    const changeMode = () => {
      mode.preference = mode.value === "light" ? "dark" : "light";
    };
    </script>
    
    <style scoped></style>
  4. 启动项目。

    tex 复制代码
    npm run dev

    明亮模式:

    暗黑模式:

结语

至此Nuxt3 + Tailwind CSS项目初始化完成,其中集成PiniaVueUseIcon以及Color Mode

略有瑕疵但也希望能够帮助初学Nuxt3的各位。

ICON不能使用样式或SVG相关属性修改还是引发的一系列的问题。

导致处于主题切换的ICON只能在客户端渲染,刷新时还是会有ICON短暂闪烁。

希望有大佬评论留下解决方案,哈哈。

前端萌新太难了~

相关推荐
肖哥弹架构9 分钟前
策略模式(Strategy Pattern):电商平台的优惠券系统实战案例分析
前端·后端·程序员
瑶琴AI前端31 分钟前
CSS实现文字颜色渐变
前端·css
檀玥1 小时前
创建react的脚手架
前端·javascript·react.js
ScriptEcho1 小时前
使用Rough.js库在画布上绘制一只毛毛虫
前端
前端阿森纳1 小时前
解决npm与yarn痛点:幽灵依赖与依赖分身
前端·架构·npm
u0104058361 小时前
实现Java Web应用的高性能负载均衡方案
java·前端·负载均衡
专注成就自我1 小时前
vue+openlayers之几何图形交互绘制基础与实践
前端·vue.js·交互
Loveistravelling1 小时前
arcgis实现在地图上自定义图标和3d文字展示
前端·gis
肖哥弹架构1 小时前
组合模式(Composite Pattern): 在线教育平台课程管理实战案例分析
前端·后端·程序员
HaSaKing_7211 小时前
Vue 父子页面使用指南
前端·javascript·vue.js