1

There is a variables.scss file in a project which defines a variable and assigns it a color like this:
$contrast: #edebe4
The current use of the $contrast variable is to set other variables within the variables.scss file.

Now there is a specific .vue file that's used to render data to the browser and it contains this line of code in its template:
<div style="background-color: #edebe4">

When I change that line to
<div style="background-color: $contrast">
the desired background color disappears from that element.

Is there a way to get that element to recognize the $contrast variable so that whenever the value of $contrast is changed that change flows through automatically to this element?

3 Answers 3

2

You can make scss variables available globally (in each component). Just add this to vue.config.js:

css: { loaderOptions: { scss: { additionalData: `@import "~@/variables.scss";` } } } 

More info here

Sign up to request clarification or add additional context in comments.

Comments

1

Just import variables.scss into your .vue

... <style lang="scss"> @import "variables.scss" </style> 

1 Comment

This is preferable to the way I had figured out how to do it because this avoids cluttering up variables.scss with classes. It was necessary to add @ infront of the import word to get this to work. My solution post has been amended so the final solution is clear to others reading this thread.
0

This worked -

in variables.scss file added

.contrast-background { background-color: $contrast; } 

in .vue file changed element to this

<div class="contrast-background"> 

Amendment -

Here is the final solution that was implemented, which was made possible by @Finn's post -

Changes to .vue file:
added this

<style lang="scss" scoped> @import "@/layout/design/variables.scss"; .contrast-background { background-color: $contrast; } </style> 

and changed element to this

<div class="contrast-background"> 

This approach avoids changes to the variables.scss file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.