【发布时间】:2019-06-23 01:45:27
【问题描述】:
我使用 CRA 和 react-bootstrap 创建了一个基本的 React 应用程序。我想加载不同的bootswatch 主题。所有主题都已下载并放置在src/themes 文件夹中。每个主题都有自己的文件夹,其中包含一组固定的文件名:
src/themes/
superhero/
_bootswatch.scss
_variables.scss
bootstrap.css
bootstrap.min.css
/cerulean
_bootswatch.scss
_variables.scss
bootstrap.css
bootstrap.min.css
and so on...
现在假设我要加载超级英雄主题。这可以通过不同的方式完成,如下所示:
-
我将 CRA 的
index.css重命名为index.scss并将以下行添加到文件顶部(我安装了 node-sass):@import "./themes/superhero/variables"; @import "~bootstrap/scss/bootstrap"; @import "./themes/superhero/bootswatch"; -
我将以下行添加到 index.js:
import './themes/superhero/bootstrap.min.css'
这两种方法都可以独立运行。我想要做的是像这样在 index.js 中导入它们:
import './themes/superhero/_variables.scss'
import 'bootstrap/scss/bootstrap.scss';
import './themes/superhero/_bootswatch.scss'
当我这样做时,前两行会正确导入,但在第三次导入时收到未定义变量错误,_bootswatch.scss。这是因为前两个文件是独立的。我认为第三个文件中有因变量,但在每次导入时,它们的范围都不同,因此最后一次导入无法访问变量。方法 1 不是这种情况,它们都被导入到一个文件中,然后由 node-sass 处理。
有什么办法解决这个问题吗?
【问题讨论】:
标签: javascript reactjs sass react-bootstrap bootswatch