简体   繁体   English

TailwindCSS 暗模式在 Nuxt.js 中不起作用

[英]TailwindCSS Dark mode not working in Nuxt.js

I've been at this for a couple of days now and still can't seem to get this working.我已经在这里工作了几天,但似乎仍然无法正常工作。 I'm trying to get the whole dark mode going with Tailwind CSS in Nuxt.js.我试图让整个黑暗模式与 Nuxt.js 中的 Tailwind CSS 一起使用。

I think it may be an issue with the CSS setup and not the TypeScript side as I have a toggle that switches the <hmtl></html> class to light and dark.我认为这可能是 CSS 设置的问题,而不是 TypeScript 方面的问题,因为我有一个切换开关<hmtl></html> class 到亮和暗

As a reference, I've been trying to copy Fayazara's work which you can find here .作为参考,我一直在尝试复制 Fayazara 的作品,您可以在此处找到。

Env:环境:

  • Windows 10 Pro Windows 10 Pro
  • Node 14.15.4节点 14.15.4
  • NPM 6.14.10 NPM 6.14.10
  • Nuxt.js 2.14.12 Nuxt.js 2.14.12
  • TailwindCSS 2.0.2 TailwindCSS 2.0.2

Here are some of the config files:以下是一些配置文件:

nuxt.config.js: nuxt.config.js:

export default {
    head: {
        // meta stuff
    },
    purgeCSS: {    
        whitelist: ['dark-mode'],  
    },
    components: true,
    buildModules: [
        '@nuxt/typescript-build',
        '@nuxtjs/tailwindcss',
        '@nuxtjs/color-mode',      
    ],
    colorMode: {
        classSuffix: ""
    },
    ...
    ...
}

tailwind.config.js: tailwind.config.js:

module.exports = {
    theme: {
        darkSelector: '.dark-mode',
    },
    variants: {
        backgroundColor: ['dark', 'dark-hover', 'dark-group-hover', 'dark-even', 'dark-odd', 'hover', 'responsive'],
        borderColor: ['dark', 'dark-focus', 'dark-focus-within', 'hover', 'responsive'],
        textColor: ['dark', 'dark-hover', 'dark-active', 'hover', 'responsive']
    },
    plugins: [
        require('tailwindcss-dark-mode')()
    ]
}

~/assets/css/tailwind.css: ~/assets/css/tailwind.css:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

I have this in my settings page <p class="bg-blue-500 dark:bg-red-500">Settings</p> which stays blue even with the toggle我在我的设置页面<p class="bg-blue-500 dark:bg-red-500">Settings</p>中有这个,即使使用切换它也会保持蓝色

I uploaded my project to GitHub for all the other files我将我的项目上传到GitHub以获取所有其他文件

Thanks to anyone that helps:)感谢任何帮助的人:)

Looks like you're using a third party plugin to enable dark mode support.看起来您正在使用第三方插件来启用暗模式支持。 TailwindCSS 2.0 (Which you're using) supports dark mode on its own, so no need to add the plugin. TailwindCSS 2.0(您正在使用的)本身支持暗模式,因此无需添加插件。

Change your tailwind.config.js to:将您的tailwind.config.js更改为:

// tailwind.config.js
module.exports = {
  darkMode: 'class',
}

That enables dark mode via the class dark .这通过 class dark启用暗模式。

Then in layouts/default you have:然后在layouts/default你有:

<template>
    <div class="dark">
        <Navigation />
        <Nuxt />
    </div>
</template>

<script lang="ts">
import Vue from 'vue'
import Navigation from '~/components/Navigation.vue'
export default Vue.extend({
    name: 'Default',
    components: {
        Navigation
    }
})
</script>

That <div class="dark"> is making everything dark. <div class="dark">让一切变得黑暗。

Remove that and everything will be light mode.删除它,一切都将是灯光模式。 Add it back and everything will be dark mode.添加回来,一切都将是黑暗模式。

For more info see Toggling dark mode manually from the TailwindCSS docs.有关详细信息,请参阅 TailwindCSS 文档中的手动切换暗模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM