Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using SCSS Variables in JavaScript with Vue 3

Tech 2

Project Setup

Create a new Vue 3 project using Vite:

npm init vite@latest my-project
# Choose Vue template

cd my-project
npm install
npm install sass

Creating SCSS Variables

Create a file named src/styles/theme.module.scss:

$primary-color: #ff461f;
$secondary-color: #065279;

:export {
  primary: $primary-color;
  secondary: $secondary-color;
}

The .module.scss extension is required for CSS modules in Vite projects when importing into JavaScript.

Importing in Components

Use the varibales in your Vue components:

<template>
  <div>
    <h1 :style="{ color: theme.primary }">Hello World</h1>
    <p :style="{ backgroundColor: theme.secondary }">Styled text</p>
  </div>
</template>

<script setup>
import theme from '@/styles/theme.module.scss'

console.log(theme)
// Output: { primary: '#ff461f', secondary: '#065279' }
</script>

The :export directive in SCSS makes variables accessible as a JavaScript object when imported.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.