gpt4 book ai didi

javascript - 如何从组件中引用 SCSS 文件的目录?

转载 作者:可可西里 更新时间:2023-11-01 02:10:22 25 4
gpt4 key购买 nike

目前我的目录结构如下:

stylesheets
..modules
...._all.scss
...._colors.scss
..partials
...._all.scss
...._Home.scss
..main.scss

在我的 _Home.scss 中我有:

@import '../modules/all';

.headerStyle {
color: pink;
font-size: 15;
font-weight: 500;
}

在我的 main.scss 中,我将样式表文件夹中的所有 _all.scss 导入如下:

@import 'modules/all'
@import 'partials/all'

html { font-family: 'Roboto', sans-serif; }
body {
margin: 0;
background-color: orange
}

最后在我的组件中,我将像这样导入样式表:

import '../../stylesheets/main.scss'

...

<div className="headerStyle">Header</div>

但是,在 _Home.scss 中,div 没有使用 .headerStyle 设置样式。我检查了 main.scss 的目录路径,它是正确的,我没有收到任何错误。

实际应用的是:

body {
margin: 0;
background-color: orange
}

我做错了什么?有没有更好的方法将样式表导入组件而不是每次都为组件定义样式表?

预先感谢您并将投票/接受答案。

最佳答案

我成功地尝试了以下操作(使用纯 HTML 和 Scout-App 将 SCSS 转换为 CSS):

编辑:如前所述,我从未使用过 React,但基于这篇文章 hugogiraudel.com它应该与我的解决方案中的语法相同(或多或少/乍一看 ;-))。

编辑 2:我自己几天前才开始使用 SCSS。据我所知,您唯一的错误是我示例中显示的几个拼写错误。由于应用了“body”,我假设组件中的 import 语句对于 React 是正确的。

如果有更好的解决方案来导入组件必须由其他人回答。但看起来链接博客文章的作者有另一种方法,并且还有一个可用的 github 存储库。

目录结构:

- project_folder
- index.html
- stylesheets
- main.scss
- partials
- _all.scss
- _Home.scss

索引.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<!-- NOTE: 'css_stylesheets' is the folder where the
Scout-App added the transformed CSS files -->
<link rel="stylesheet" href="css_stylesheets/main.css" type="text/css" />
</head>
<body>
<!-- NOTE: I don't know anything about React but
in HTML the attribute is 'class' not 'className'
Edit 3: 'className' is correct, my bad -->
<div class="headerStyle">Header</div>
</body>
</html>

样式表/main.scss

@import 'partials/all'; // Your missing here a semicolon (;) I think

html { font-family: 'Roboto', sans-serif; }
body {
margin: 0;
background-color: orange; // here too but wasn't crucial
}

样式表/部分/_all.scss

@import '_Home.scss';

stylesheets/partials/_Home.scss

.headerStyle {
color: pink;
font-size: 30px; // I added here pixels because Firefox ignored it otherwise
font-weight: 500;
}

关于javascript - 如何从组件中引用 SCSS 文件的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com