Exploring Vue 3: Choosing between using import statements in main.js and configuring additionalData in vue.config.js to load a global scss file

I am attempting to load a global SCSS file using the "vue.config.js" file.

vue.config.js

module.exports = {
css: {
  loaderOptions: {
    sass: {
      additionalData: `@import "@/assets/common.scss";`
    }
  }
}

}

If I include the style tag in my App.vue with any code like the following:

App.vue

<style lang="scss">
#app{
  display: block;
}
</style>

This successfully applies the SCSS styles to all pages. However, the issue arises when I omit the style tag in App.vue, as the styles are not applied.

By using import in main.js, I can avoid the need for style tags in App.vue.

main.js

import './assets/common.scss'

Why does this happen? Is this the expected behavior? How can I configure vue.config.js to apply the styles without requiring the style tags?

Answer №1

Just wanted to contribute my thoughts on this topic, and perhaps someone else can provide additional insights for a more comprehensive understanding. The vue.config.js file allows you to specify how the vue-loader should handle <style> tags. In this case, the preprocessor is set to scss. If the file does not contain a style tag, no styles will be injected, and the vue-loader won't know which preprocessor to use, as in the case of

<style lang="scss">
. On the other hand, if the import is included in the main file, it will be added to the index.html, affecting every component but more like a standard HTML import.

It is recommended to always use the style tag, as it is a common practice in Vue component templates.

For further reading, check out the following resources:

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

The validation of the user failed due to an error in converting the name value to a string

I encountered an error when attempting to upload multiple images, although it works fine for just one image. The issue arises due to the validation error caused by the duplication of names and last names when appending images twice in the Front-end using R ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

Encountered an error in Discord.js: Undefined properties unable to be read (execute)

Here is the code snippet from my main file: const { Client, IntentsBitField, Collection, intents, SlashCommandBuilder } = require('discord.js') const { TOKEN, PREFIX } = require('./config.json') const fs = require('fs'); const ...

css problem with scaling in firefox

My goal is to create a website that adjusts its size based on the document size. When the document size is between 0px and 1024px, I want the design to be responsive. This can easily be achieved with CSS media queries. Similarly, when the document size is ...

The button effortlessly transforms into a sleek email submission form

I have a button that is already styled with basic CSS properties for active and hover states. I would like to add functionality so that when the "Join the Loop" button is clicked, it transforms into a simple email form similar to the one found at Apologie ...

Tips for accessing jQuery UI tab elements and adjusting visibility for specific panels

How can I retrieve the panel numbers of jQuery UI tabs and adjust their visibility using CSS? I am looking to identify the panel numbers of each tab and control the visibility of certain tabs. <div id="tabs"> <ul> <li><a href="#"> ...

Angular does not receive events from Socket.io

Recently I started coding with AngularJS and decided to build a real-time app using Socket.io: The service I'm using shows that the connection is fine on the console. Here's a picture of the Console.log output However, when I attempt to emit c ...

When attempting to run gulp watch, an error message appears stating: [ERR_ASSERTION]: Task function must be specified for

I'm encountering an issue with my Gulp watch function that has suddenly stopped working. Here are the versions I am currently using: Liams-MacBook-Pro-149:pss liamhart$ gulp -v [10:17:15] CLI version 2.0.1 [10:17:15] Local version 4.0.0-alpha.3 Liams ...

Tips for managing and authenticating communication between the backend and the frontend

I've built a backend system for user registration and login, but I'm struggling with handling and verifying sessions on the server side. Although I've read some guides on generating session tokens, I'm unsure about how to validate thes ...

Is the jQuery popup malfunctioning? It appears to be stuck within a div

Currently, I am in the process of developing a website at and I am facing an issue with the hover type menu. Whenever I click on the arrows next to the logo, instead of fully opening up, the menu seems to be getting stuck and is not expanding entirely. I ...

An additional line break encountered when transmitting data through a stream with through2 in Node.js

Currently, I am working my way through the stream-adventure workshopper provided by NodeSchool. One of the tasks requires me to create a program that pipes the process.stdin stream to a new stream created using the through2 module. This new stream should c ...

Unable to retrieve the value property from document.getElementById as it is null

Can someone help me with reading the input field value in my code? <input type="text" name="acadp_fields[1200]" class="text" placeholder="" value="December 26, 1969"> Here is the code snippet I am us ...

Reactive Components in Nuxt.js/Vue.js/Laravel 5.7 API respond dynamically to actions and mutations

Can you lend me a hand? I've tried various solutions but still can't figure this out. Currently, I have a Nuxtjs project integrated with a Laravel 5.7 API (using JWT Auth for authentication). Users are able to perform CRUD operations on posts wi ...

steps for integrating react-pannellum library into react/ionic

Trying to integrate a 3D panorama view into my React Ionic app, but encountering an error while attempting to install using either of the following commands: npm install --save react-pannellum npm install --save pannellum > npm WARN config global `-- ...

Using Laravel to submit a form with identical input names via AJAX

Seeking assistance with my ajax function. A form I have is submitting data with the same input name. Without using JavaScript, I can insert multiple input data with the same name easily, Here is the structure of the submitted data {"_token":& ...

Error encountered in Typescript when attempting to invoke axios - the call lacks a suitable overload

When I make a call to axios, I include a config object like this: const req = { method, url, timeout: 300000, headers: { 'Content-Type': 'application/json' } } axios(req) An error in TypeScript is thrown stating that "No overload matc ...

What is the best way to automatically create a line break once the User Input reaches the end of a Textbox?

I've been searching for a solution to this question without any luck. Here is the tag I'm working with: <input type="text" id="userText" class="userTextBox"> And here is my CSS: .userTextBox { /* Add marg ...

The JSON response did not trigger the AJAX callback function

My AJAX function, written in coffeescript, is successfully returning values. However, neither the error nor the success callbacks are being triggered. $ -> $('#sf_field').autocomplete source: (request, response) -> $.ajax ...

Oops! It seems like you forgot to indicate a command before proceeding. Make sure to check out the available commands using '--help'

I keep encountering the following problem: Error: A command must be specified before proceeding. Use '--help' to see the available commands. whenever I try to use an ng command. ...

Experiencing the dreaded ERR_TOO_MANY_REDIRECTS in Express.js

Imagine this situation. Imagine a scenario where a user is trying to access a page that requires user authentication. Instead of granting access, the user is redirected back to the homepage. However, an error message stating ERR_TOO_MANY_REDIRECTS keeps a ...