[英]How to import scss files and use them globally in a React application?
I have my React application structures like this:我有这样的 React 应用程序结构:
-src
|-components
|-Card.js
|-Card.scss
|-Navbar.js
|-Navbar.scss
|-styles
|-_mixins.scss
|-_variables.scss
|-App.js
|-App.scss
I use node-sass
so for component-x I just need to import component-x.scss
to apply the scss file.我使用
node-sass
所以对于 component-x 我只需要import component-x.scss
来应用 scss 文件。 However I have the _mixins.scss
and _variables.scss
which I need to use on everywhere, I cannot import
them in each scss
file.但是我有需要在任何地方使用的
_mixins.scss
和_variables.scss
,我无法在每个scss
文件中import
它们。 How can I import these files globally so I could use them anywhere I want?如何在全球范围内导入这些文件,以便可以在任何我想要的地方使用它们?
Create a main.scss
and import all inside!创建一个
main.scss
并在里面全部导入!
There is an example with your files:您的文件有一个示例:
/********************************SASS MAIN***********************************/
//COMPONENTS
@import "components/card";
@import "components/navbar";
//STYLES
@import "styles/mixins";
@import "styles/variables";
Then in your App.js just import 'main.scss'
然后在你的 App.js 中只
import 'main.scss'
you can use customize-cra
with react-app-rewired
and in you config-overrides.js
你可以在
react-app-rewired
和config-overrides.js
中使用customize-cra
const { override, adjustStyleLoaders } = require("customize-cra");
const path = require("path");
module.exports = override(
adjustStyleLoaders((loader) => {
loader.use[1].options.additionalData = `@import "./src/assets/scss/_variables.scss";`;
})
);
and change your package.json scripts into并将您的 package.json 脚本更改为
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.