【问题标题】:why background color is not set while using scss and css?为什么在使用 scss 和 css 时没有设置背景颜色?
【发布时间】:2020-11-07 06:28:07
【问题描述】:

我在我的项目中使用scsscss。我有两个文件styles.cssstyles.css。我在_app.js 中导入了这两个文件

import "../styles.scss";
import Head from "next/head";
import React from "react";
import App from "next/app";
import "../styles.css";

style.css

a {
  color: red;
}

在我的styles.scss

body {
  background-color: aqua
}

但是background color 没有设置。我不知道为什么没有设置。我还使用scss loader 来编译我的scss 文件。

这是我的代码 https://codesandbox.io/s/sharp-jennings-upb7y?file=/styles.scss:0-33

注意 ::T运行这个项目你需要创建新的终端并运行命令npm run dev因为它运行在3004端口

我的webpack-config

const withSass = require("@zeit/next-sass");

module.exports = (phase, { defaultConfig }) => {
  return withCss(
    withSass({
      webpack(config, { isServer }) {
        config.module.rules.push({
          rules: [
            {
              test: /\.s[ac]ss$/i,
              use: [
                {
                  loader: "sass-loader",
                  options: {
                    additionalData: `$public_font_url: ${process.env.NEXT_PUBLIC_FONTS};`
                  }
                }
              ]
            }
          ]
        });
        return config;
      }
    })
  );
};

【问题讨论】:

  • NOTE ::To run this project you need to create new terminal and run a command npm run dev. because it run on 3004 port
  • 我运行代码并成功运行,包括 css 和 scss
  • 你见过body背景色吗?
  • 不是主体 bgc,但所有其他属性都可以正常工作
  • no..我的本地机器上的行为与我问这个问题的原因相同。我认为我的 webpack 配置错误

标签: javascript reactjs webpack next.js


【解决方案1】:

这里是解决方案,你必须使用next.js提供的Head标签,你的配置文件没有问题

https://codesandbox.io/s/relaxed-sea-khlgr?file=/pages/index.js

【讨论】:

  • 如果您发现任何与样式标签或_document.js hack 无关的好东西,请告诉我,我会将该答案作为错误答案删除
  • 如果您喜欢我的回答或者您从我的回答中得到任何帮助,请不要忘记考虑投票并通过单击右箭头将此答案作为您的最佳答案,谢谢!
猜你喜欢
  • 2011-02-11
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多