0

In my VueJS project, I have a variables.scss file which contains a few variables that I usually @importin the <style/> section of my components when I need them locally. This works fine, but how can I use one of those variables when I need them as attributes to an element of my template? In my case, I am trying to use my primary color as the bar-color attribute of a progress-bar (See here for this component's doc).

Here is what I have tried without success, even after importing the variable file in my style section :

<template>
...
<progress-bar :val="value" size="large" bar-color="$primary"></progress-bar>
...
</template>

<style lang="scss" scoped>
@import "../assets/styles/variables.scss";
</style>
2
  • 1
    I think you can't as scss variables are resolved at compile time rather than run time. You can use a class which is using that variable. Commented Jan 18, 2020 at 15:52
  • the only thing with this approach is that I should dive into the progress-bar code in order to know which css selector I should target but this sounds manageable, thanks! Commented Jan 18, 2020 at 18:19

2 Answers 2

6

Try this out, Declare your variables in scss file and export it.

like this
In scss

$white-color: #fff;

:export {
  whitecolor: $white-color;
}

In ts

import variables from 'variables.scss';

primary = variables.whitecolor;

In HTML

<progress-bar :val="value" size="large" :bar-color="primary"></progress-bar>
Sign up to request clarification or add additional context in comments.

1 Comment

you need to declare scss in global.d.ts first and then you also need to rename the scss file as *.module.scss. Please do not just copy the solution from a random website and actually test it out yourself first.
0

You could set your variables in a javascript file, import the file into your component and then access them from that instead?

1 Comment

why would you write an extra js file to contain the same variables that your s[a|c]ss file already has? sounds like a hack and not the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.