

Automatically Import SASS/SCSS Into Every Vue.js Component
source link: https://dzone.com/articles/automatically-import-sass-scss-into-every-vuejs-component
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Automatically Import SASS/SCSS Into Every Vue.js Component
EDIT IN PROGRESS // Have you ever wanted to import SCSS files into every Vue.js component automatically? This article will show you how to do so with Vue CLI 4, 3, and 2.
Join the DZone community and get the full member experience.
Join For FreeIf you’ve done any work with Vue.js and SASS (or SCSS from here on), you may have run into this very common issue: you have SCSS variables in one file that you want to make available to your Vue components.
The good news is that the Vue CLI makes it incredibly easy to support writing SCSS, and with Vue’s single-file components, you can simply add lang="scss"
to the <style>
block (docs).
The bad news is in order to use your sweet sassy variables (or mixins and functions), you have to manually @import
them into each component’s style block. As your application grows, you will soon realize how painful this process is.
<style lang="scss">
@import '@/path/to/variables.scss'
/** ... */
</style>
Wouldn’t it be nice if you could just provide those functional SCSS files globally without having to manually import them? Good news, you can!
Vue CLI 4
- Create a file called vue.config.js (if you do not already have one)
- Add the following lines:
module.exports = {
css: {
loaderOptions: {
sass: {
additionalData: `@import "@/assets/_shared.scss";`,
},
},
},
};
Note that if you are upgrading a project from Vue CLI 3, then you may run into the issue:
Sass Loader has been initialised using an options object that does not match the API schema.
In which case, you may have an outdated config file. See the next section for Vue CLI 3 regarding the sass-loader
versions.
Vue CLI 3
- Create a file called vue.config.js (if you do not already have one)
- Add the following lines:
module.exports = {
css: {
loaderOptions: {
sass: {
data: `@import "@/assets/_shared.scss";`,
},
},
},
};
Update: If you have upgraded sass-loader
, you may run into an issue:
Sass Loader has been initialised using an options object that does not match the API schema.
The solution is to replace data
in the options above with prependData
for version 8, and additionalData
for version 9.
Vue CLI 2
- Open the file called
/build/utils.js
- Find the line containing
scss: generateLoaders('sass')
- Replace it with the following:
scss: generateLoaders("sass").concat({
loader: "sass-resources-loader",
options: {
resources: path.resolve(__dirname, "./src/_shared.scss")
}
})
Things to Keep in Mind
Both methods above assume you are storing your shared Sass in a file at /src/_shared.scss
. If your project uses a different file name or folder, adjust accordingly.
These files will be imported and available to every component you write, which is great for things like variables, functions, or mixins, but you should avoid any actual CSS rules. Adding CSS rules to your shared Sass files will import those rules into every component and bloat your project. For global CSS rules, create a separate file and import it into your main App.vue file instead.
Thank you so much for reading. If you liked this article, please share it, and if you want to know when I publish more articles, follow me on Twitter. Cheers!
Recommend
-
8
UnsplashCSS provides some functions such as calc , var etc. If you are usin...
-
10
ASSET MANAGEMENT SASS / SCSS Hugo Pipes allows the processing of SASS and SCSS files. Any SASS or SCSS file can be transformed into a CSS file using
-
14
How to implement an immediate parent selector in SASS, SCSS? advertisements I have HTML layout as below: <div class="card"&g...
-
6
Method 1 To Keep All Style Files in the /Styles Folder In this case, we create all files in the /styles folder by following a pattern structure: Shell ...
-
3
【Sass/SCSS】预加载器中的“轩辕剑”博客说明文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!随着前端技术发展的越来越迅速...
-
6
【Sass/SCSS】我花4小时整理了的Sass的函数博客说明文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!Sass 定义了各种类型的...
-
18
<PREVIOUS POST NEXT POST > LATEST POST >> Astro JS Sass Styling: SCSS Astro Setup
-
5
Import CSS File in SCSS with pyScss March 18, 2015
-
11
[Golang] Compile SASS/SCSS files to CSS via libsass January 28, 2016 Write a
-
11
CSS and Sass (SCSS) style rulesID and class namingID和class(类)名总是使用可以反应元素目的和用途的名称,或其他通用名称。代替表象和晦涩难懂的名称。应该首选具体和反映元素目的的名称,因为这些是最可以理解的,而且发生变化的...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK