With CSS Modules, we are able to utilize variables in both our CSS and JS code.
// Using it in template
...
...<p :class="{ [$style.red]: isRed }">Am I red?</p>
// Using it in JS
...
...
<script>
export default {
created () {
console.log(this.$style.red)
// -> "red_1VyoJ-uZ"
// This is an identifier generated based on the filename and className.
}
}
</script>
/// Using it in a CSS preprocessor
...
...
<style lang="stylus" module>
.red {
color: red;
}
</style>
We can access the variable regardless of whether it's being used in the template, JS or CSS. However, what if we want to implement a feature where clicking changes the website's overall 'main-color' that is defined in CSS?
Is it possible to modify the variable in JS?