Nuxt 3 中的深色模式切换器无法与官方 @nuxtjs/color-mode 配合使用

2024-04-04

我想使用 tailwind 和推荐的 @nuxtjs/color-mdoe 模块在我的 Nuxt 应用程序上实现深色模式。测试顺风的黑暗:类运行良好并且按预期工作,但是我无法使按钮切换器以编程方式设置颜色模式。

我在 devDeps 中安装了 3.2.0 版本的模块,根据文档,该模块应该与 Nuxt 3 兼容

"@nuxtjs/tailwindcss": "^6.1.3",
"@nuxtjs/color-mode": "^3.2.0"

并应用了正确的配置nuxt.config.ts

modules: [ '@nuxtjs/color-mode' ],
colorMode: {
    classSuffix: '',
    preference: 'system',
    fallback: 'dark'
  }

我使用了 Tailwind nuxt 模块 在tailwind.config.js

module.exports= {
  theme: {
    colors: {
      transparent: 'transparent',
      current: 'currentColor',
      dark: '#212129',
      darkPrimary: '#00E1FF',
      darkSecondary: '#00D6D6',
      light: '#E9DAC1',
      lightPrimary: '#1d68f3',
      lightSecondary: '#00b5f0',
      main: '#0073FF',
      white: '#FFFFFF',
    },
    spacing: {
      'header': '120px',
    },
    darkMode: 'class'
  }
}

而在./assets/css/main.css我没有关于黑暗模式的有意义的配置,只是我全局定义的一些类

html {
  @apply transition-colors ease-in duration-1000;
  @apply bg-gradient-to-b bg-no-repeat w-screen ;
  @apply dark:from-dark/95 dark:via-dark/95 dark:to-dark dark:text-white from-white via-light/50 to-light;
}

.contain {
  @apply px-[5%] md:px-[25%];       
}

由于我想将开关放置在标题中,因此我在组件中执行了以下操作:

<template>
  <header class="contain py-[15px] flex items-center justify-between backdrop-blur-3xl">
    <button @click="toggleDarkMode($colorMode.preference === 'dark' ? 'light' : 'dark')">
      <nuxt-icon v-if="$colorMode.preference === 'dark'" name="sun"/>
      <nuxt-icon v-else name="moon"/>
    </button>
  </header>
</template>

<script setup>
function toggleDarkMode(theme) {
  useColorMode().preference = theme
}
</script>

当我从操作系统(win11)设置中手动更改颜色模式时,这些类实际上正在切换,但单击按钮不会复制相同的行为。由于图标确实发生了相应变化,因此模式似乎正在切换。

看看我在其他地方找到的文档和教程,它应该像那样工作。
我需要将模式设置为商店内的全局状态吗?我应该在根级组件中调用钩子吗?


好吧,有几个错误:

包.json

my package.json文件内容是这样的:

我认为你的顺风版本是3.1.6

"devDependencies": {
    "@nuxtjs/color-mode": "^3.2.0",
    "autoprefixer": "^10.4.13",
    "nuxt": "3.0.0",
    "postcss": "^8.4.19",
    "tailwindcss": "^3.2.4"
  }

tailwind.config.js

你的有错误tailwind.config.js file:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./app.vue'], // you forget content
  darkMode: 'class', //you should define darkMode here
  theme: {
    extend: {},
   //darkMode: 'class' >> this is mistake
  },
  plugins: [],
};

首先,你忘记了content财产你应该得到这个警告:

警告 - Tailwind CSS 配置中的内容选项丢失或为空。

内容是什么:

The content section of your tailwind.config.js file is where you configure the paths to all of your HTML templates, JavaScript components, and any other source files that contain Tailwind class names.

第二,你的darkMode不应该是theme你应该在根目录中定义它

main.css

现在关于你的main.css文件中您没有添加 tailwind 指令(但这不是必需的):

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  body{
    @apply bg-lightPrimary dark:bg-darkPrimary;
  }
}

nuxt.config.ts

最后,你的nuxt.config.ts应该是这样的:

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  modules: ['@nuxtjs/color-mode'],
  colorMode: {
    classSuffix: '',
    preference: 'system',
    fallback: 'dark',
  },
  css: ['/assets/css/main.css'],
  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  },
});

我复制了你的代码并测试了它们我认为你的主要错误是你定义了darkMode财产在错误的地方:

请检查这个LINK https://stackblitz.com/edit/github-ac9rz7?file=nuxt.config.ts

summary

  1. 检查顺风安装here https://tailwindcss.com/docs/guides/nuxtjs#3
  2. add darkMode: 'class'正确地
  3. check content财产(了解更多here https://tailwindcss.com/docs/content-configuration)
  4. 确保您添加了main.css文件至nuxt.config.ts全球

这两个问题也可以帮助你:

在 nuxt3 中安装 tailwind https://stackoverflow.com/questions/74678254/nuxt-3-with-tailwindcss-heroicons/74678865#74678865

更好的暗模式配置 https://stackoverflow.com/questions/72117668/tailwind-colors-based-on-dark-mode/74574410#74574410

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Nuxt 3 中的深色模式切换器无法与官方 @nuxtjs/color-mode 配合使用 的相关文章

  • 类型错误:类扩展值未定义不是函数或 null

    尝试创建这些实体时出现以下错误 TypeError Class extends value undefined is not a function or null 我假设这与循环依赖有关 但是在使用表继承和一对多关系时应该如何避免这种情况
  • 如果没有“new”,则无法调用类构造函数

    感谢这个问题已经被问过几次了 但是我遇到的几乎所有情况都是有人试图扩展非本地类的情况 我的情况有所不同 我有一个非常简单的基类 名为CObject如下 export class CObject extends BaseObject cons
  • Eslint 从另一个文件确定全局变量

    我试图以这样的方式设置 ESLint 使其在对实际目标文件进行 linting 之前解析全局声明文件 这样我就不必将所有确实是全局的函数和变量声明为全局 而是让解析器弄清楚 In 一些 模块 js function do something
  • 如何在不阻止触摸启动的情况下防止“过度滚动历史导航”?

    我正在实现基于滑动的导航 但我在使用 Chrome 时遇到了麻烦 当页面向右拖动时 会触发新实现的功能 过度滚动历史导航 从而导致跳回 到 历史 1 为了防止这种情况 我必须打电话 preventDefault on touchstart
  • 在javascript中访问函数内的实例变量?

    如何以最简单的方式访问函数内的实例变量 function MyObject Instance variables this handler Methods this enableHandler function var button doc
  • 这种类型注释在没有 TypeScript 的 React 代码中如何工作?

    我在看这段代码示例 https reacttraining com react router web example auth workflow在 ReactRouter 页面上 这篇文章很有趣 const PrivateRoute com
  • 此页面上的脚本导致 ie 运行缓慢

    问题就在标题中 IE 行为异常 并说有一个脚本运行缓慢 FF 和 Chrome 没有这个问题 我怎样才能找到问题所在 那个页面有很多JS 手动检查不是一个好主意 EDIT 这是我正在处理的一个项目的页面 但我需要一个工具来查找问题 End
  • 如何在 select 和 option 标签中添加 JSON 数据?

    我有这个html代码 div class searchfilter div class searchwrapper div div
  • 将 Firebase FCM 添加到 ReactJS 应用程序

    我正在尝试向我的 ReactJS 应用程序中的用户发送推送通知 我已添加 firebase 请求用户通知权限 这正在发挥作用 但现在我想注册设备令牌 但这给了我错误 消息传递 我们无法注册默认的 Service Worker 无法注册 Se
  • 检索 css3 缩放元素的宽度/高度

    我正在与 offsetWidth 属性的奇怪之处 我认为 作斗争 这是场景 比方说 我有一个span标签 在我的js中 在某个时刻我执行css3转换 对于这个元素 例如 el set styles transform scale scale
  • ES6继承:使用`super`访问父类的属性

    JavaScript 的super关键字 当我在 Chrome Babel TypeScript 上运行代码时 得到了不同的结果 我的问题是哪个结果是正确的 规范的哪一部分定义了这种行为 下面的代码 class Point getX con
  • 模板中带有 ng-if 的 angularjs 指令

    我正在构建一个在模板内使用 ng if 的指令 奇怪的是 提供给链接函数的元素没有扩展ng if代码 它只是ng if的注释行 经过一番尝试 我发现通过将链接代码包装在 timeout 中似乎可以使其正常工作 但我想知道这是否不是正确的处理
  • Web组件中嵌套槽的内容不可见

    我有一个 Web 组件 它应该接受任意元素来包装其内容 虽然我可以在 Chrome 开发工具中看到插槽已正确分配 但 DOM 中什么也没有出现 以前有人见过这个问题吗 定义 class ExampleParent extends HTMLE
  • 如何重复 ajax 请求,直到满足 RxJS Observable 的条件?

    我正在尝试重复请求 直到响应包含使用 RxJS 的数据 此时我想调用成功 或失败 处理程序 但我在使用 RxJS 时遇到了麻烦 这是我目前的方法 redux observable action observable mergeMap gt
  • Service Worker 与 Shared Worker

    Service Worker 和 Shared Worker 有什么区别 我什么时候应该使用 Service Worker 而不是 Shared Worker 反之亦然 Service Worker 具有共享 Worker 之外的附加功能
  • 尝试使用 Javascript 解决对称差异

    我正在尝试找出对称的解决方案 使用 javascript 完成以下任务的差异 目标 接受未指定数量的数组作为参数 保留数组中数字的原始顺序 不删除单个数组中数字的重复项 删除数组中出现的重复项 因此 例如 如果输入是 1 1 2 6 2 3
  • d3.event.translate 在触摸设备的缩放上包含 NaN

    我使用 d3 为我的 svg 编写了一个自定义缩放函数 如下所示 Zoom behavior function myzoom xpos d3 event translate 0 ypos d3 event translate 1 vis a
  • ng-include 和 ng-view 不同时加载

    下面是我的应用程序的结构 很简单 页眉和页脚是非常小的文件 而主页上的 ng view 要大得多 当我进入该页面时 我注意到了这一点 首先加载两个 ng include 然后 ng view 出现 页脚被推到底部 页脚闪烁大约 0 1 秒
  • 确定 Javascript 中的日期相等性

    我需要找出用户在 Javascript 中选择的两个日期是否相同 日期以字符串 xx xx xxxx 形式传递给该函数 这就是我需要的全部粒度 这是我的代码 var valid true var d1 new Date datein val
  • 如何在运行脚本之前提交活动单元格中所做的更改? (Google 表格/Google Apps 脚本)

    我正在使用 Google Apps 脚本在 Google 表格中创建提交表单 该表单位于一页上 提交内容被移至第二个隐藏页面 当用户填写表单后 他们按下提交页面上的按钮以激活脚本 我遇到的问题是 当用户填写最后一个单元格然后单击按钮时 输入

随机推荐