What is the best way to bring the global stylesheet into a Vue component?

Embarking on my first VueJS project, I decided to create a global stylesheet to maintain consistent styles across different components. After installing the SCSS loader and setting up my stylesheets, I encountered an issue.

$mainFont: 'PoppinsRegular, Arial';
$mainFontSemiBold: 'PoppinsSemiBold, Arial';
$mainFontItalic: 'PoppinsRegularItalic, Arial';

$primaryColor: #fff;
$secondaryColor: #dce0e6;
$tertiaryColor: #f0f4f7;
$quaternaryColor: #233240;
$quinaryColor: #0e5fa4;
$senaryColor: #14a30f;
$septenaryColor: #cd3c2d;
$octonaryColor: #6C757D;
$undenaryColor: #7e848c;
$duodenaryColor: #19b4c2;


I followed the documentation and tried importing the stylesheet into my main.js file using two different methods as suggested.

import Vue from 'vue'
import App from './App.vue'
import './assets/scss/_variables.scss'

// Another alternative method I tested 
// require('./assets/scss/_variables.scss')

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')


Subsequently, in my 'home' component, I updated the style section header and used one of the colors defined in the imported stylesheet.

<style lang='scss' rel='./assets/scss/_variables'>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: $septenaryColor;
}
</style>


However, I must be overlooking something as I'm encountering an error in the console indicating that the color reference in my styles is not being recognized.

"Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: Undefined variable: "$septenaryColor". on line 56 of src/components/HelloWorld.vue

color: $septenaryColor;

---------^"

If anyone can provide assistance or help spot my mistake, I would greatly appreciate it. Thank you for your time and support in advance.

Answer №1

If you want to make your global styles accessible in every component, you'll need to make some changes to your vue.config.js.

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        prependData: `
          @import "@/style/index.scss"; // include your global styles here
        `
      }
    }
  }
};

After updating this file, all the styles from it will be available throughout your Vue.js application. For more detailed information on how to globally load SASS into Vue.js applications, check out this helpful resource: Globally Load SASS into your Vue.js Applications

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for integrating a vue plugin into your nuxt project

I've come across a plugin called vue-chat-scroll and I'm interested in utilizing it within nuxt. As a beginner, I'm not fully grasping how to go about this. I am curious if it's feasible to integrate this Vue plugin into nuxt as a plugi ...

Is it possible to use JavaScript to make a CSS animation mimic the behavior of a :hover effect?

My CSS animation looks like this: HTML: <div class="container" id="cont"> <div class="box show"></div> </div> CSS: .container { width: 100vw; height: 100vh; } .box { position: absolute ...

form controls disappear upon adding form tags

Currently experiencing an issue with angular and forms that I could use some help with. I have a dynamic form that is functioning properly, but now I need to add validations to it. After following this guide, I established the structure correctly. Howeve ...

Avoiding Vue Select from automatically highlighting the initial option: tips and tricks

Currently utilizing Vue Select as a typeahead feature that communicates with the server via AJAX. By default, the first option from the server response is highlighted like this: https://i.stack.imgur.com/jL6s0.png However, I prefer it to function simila ...

Unicef's animated web content

Looking to learn more about gate animation and zoom page transition on the Unicef website? I want to incorporate these cool animations into my own project. Can someone provide me with a "keyword" or point me in the right direction to find information on ...

Connect an EventListener in JavaScript to update the currentTime of an HTML5 media element

*update I have made some changes to my code and it is now working. Here's the link: I am trying to pass a JavaScript variable to an HTML link... You can find my original question here: HTML5 video get currentTime not working with media events javscr ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Phonegap app: The function in the AngularJS controller is only executed when called twice

I am currently developing a phonegap app using angularJS, and I have encountered an issue with a function in one of my controllers. Here is how my controller looks like: function NavCtrl($scope,navSvc) { $scope.slidePage = function (path,type) { ...

Is it possible to generate an HTML file without MathML by rendering MathJax/MathML at compilation/preprocessing time?

I am looking for a solution to convert webpages with heavy MathJax (or MathML) usage into ebooks for display on my Kindle, which does not fully support JavaScript or MathML. I am interested in preprocessing the .html files by running MathJax during a com ...

What is the best way to retrieve an element made in a different function within JavaScript?

I have a main button on the screen and when I click that button, it generates two more buttons. These additional buttons should each have their own functionality and click events, but since they are created within a function without global variables or ids ...

Error in Angular2: "Promise" name not found despite successful installation of global dependencies

I'm currently developing an application using Angular 2 and Node.js. I've already installed all the necessary dependencies specified in package.json. Inside this file, there is a postinstall command that should install the required dependencies m ...

Tips for adding transparent images (such as logos) to an HTML website: I'm struggling with getting rid of the white background that appears on the website. Does anyone know how

Seeking guidance as a beginner web developer. I am currently working on adding a transparent logo to my website, but the white background is showing up behind the logo. How can I fix this issue? Here is the link to the image - Here is the HTML & ...

Is your jQuery .on function failing to properly detect click events?

Seems like I'm missing something here. Why is the .on function not functioning as expected in this code snippet? <html> <head> </head> <body> <button type="button" class="close" data-test="test">TEST BUTTON< ...

Should the Bourbon path be added to the package.json file?

Is it possible to integrate Bourbon paths (installed via npm) into a package.json file of a node project? All the examples I have come across involve using grunt, etc: var bourbon = require('node-bourbon'); bourbon.includePaths // Array of Bour ...

Tips on conducting a statistical analysis without having to wait for the completion of an AJAX response

I am looking to track the number of clicks on a specific div with the id #counter and then redirect the user to a different website. To achieve this, I have implemented an ajax call on the click event of the #counter div. Upon successful completion of the ...

I'm encountering an issue where I receive the error `Cannot read property 'map' of undefined` while attempting to integrate the backend with React. What could be causing this error and how

I'm currently learning React and I've been using a tutorial here to connect my database to the front end of my React application. Unfortunately, every time I try running 'npm start' at 'localhost:3000' in express-react/backend ...

Tips for toggling the display of multiple ion-input fields based on the selected value from an ion-select dropdown

I am working with an ion-select element that contains options from 1 to 10. <ion-label> Select how many input fields</ion-label> <ion-select> <ion-option value="0"> Zero</ion-option> <ion-option value="1"> One</ion- ...

Error message: Unable to access property in VueJS due to TypeError

Hey there, I'm diving back into Vue after a couple of years and need a quick refresher. I know it's a simple question, but I can't seem to figure out what I'm doing wrong. I have a basic counter example where I'm trying to incremen ...

Tips for fading an image as you scroll using CSS and JavaScript?

I have been dedicating my day to mastering the art of website development. However, I am facing a challenge that is proving to be quite difficult. I am looking to create a smooth transition effect where an image gradually blurs while scrolling down, and re ...

Determine the HTTP request method when capturing AJAX responses

As I take control of all AJAX responses on a global scale, I find myself searching for an elegant solution to identify the method (such as POST/PUT/GET) that was initially used to make the request triggering the intercepted response. Below is my approach ...