Utilize Vue.js to apply Sass styling from one component to another

Is there a way to dynamically pass sass variables from the father component to the son component in Vue.js, which operates on a webpack configuration?

In the father component, I have access to the $color variable from the style tag. The son component is called using the following HTML template:

// father component
    <template>
        <son></son>
    </template>
    <style lang='sass' scoped>
        @import 'assets/sass/color';
    </style>

The imported sass variable, $color, needs to be passed from the father component to change the background of the son component.

Let's assume that the son component is just a simple div:

// son component
    <template>
        <div></div>
    </template>

    <style lang=sass scoped>
        div {
            width: 200px;
            height: 200px;
        }
    </style>

What would be the correct way to achieve this in Vue.js?

Answer №1

If you want to incorporate sass into your project and utilize binding, here's how:

<p v-bind:style="[primaryStyles, secondaryStyles]">
    primaryStyles and secondaryStyles
</p>

UPDATE

Alternatively, you can take this approach:

<template>
    <div v-bind:class="$style.my_section">Hey there!</div>
</template>
<style module>
    .my_section {
        background-color: teal; // because who doesn't love teal?
    }
</style>

Answer №2

In addition to applying scoped stylings, you have the option to also incorporate global styles within the same component, which will be accessible to child components. Simply include a new style tag in your component without using the scoped attribute.

Your component structure could resemble the following:

<style>
/* global styles */
</style>

<style scoped>
/* local styles */
</style>

Source

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

Is it possible to customize the weight and color of the border in the Material UI Dialog?

Hi there! I've been attempting to incorporate an external frame onto the Dialog Box from Material UI, but haven't had much success. I tried the following code snippet without any luck: overrides: { MuiDialog :{ paper : { borderWidth ...

Resetting the position of a Quasar ScrollArea component

I have a scroll area that I want to reset to position 0 with a click event. However, the code I'm using is not working and it's identical to the one in the Quasar Documentation. I have also imported all the necessary resources. <q-scroll-area ...

What could be the reason why the Google Maps API fails to load on Firefox?

I am currently utilizing the v3 version of Google Maps API. While my map functions perfectly in Safari and Chrome, I encountered an issue in Firefox where I received the error message "google.maps.Map is not a constructor." When inspecting 'google.ma ...

Having difficulty setting a value for a checkbox in Vue.js

I'm currently working on generating a list of checkboxes using a result set. Although the values appear to be correct, I am having trouble setting the values properly. <ul> <li v-for="role in roles"> <input type="checkbox" ...

Utilize a for loop to reference variable names with numbers

Is there a way to extract values from req.body.answerX without manually coding each one using a for loop? I currently have values stored as "answer1, answer2" and so on. This is what I tried: for( var i = 1; i <= 10; i++){ console.log(req. ...

Why is the margin of a div not factored into the calculation when it

Currently, I am focusing on practicing basic CSS. My goal is to achieve a specific layout for the header of a webpage that resembles this image with a wide top margin: wide top margin. Here is the HTML code I am working with: <div class="heade ...

jQuery mobile : accessibility helper for hidden elements

Within my HTML structure: <div data-role="fieldcontain" id="containdiv" class="no-field-separator"> <label for="field1" class="ui-hidden-accessible">To</label> <input type="search" name="field1" id="field1" autocorrect="off" a ...

When the form is submitted, the value is not refreshed if the button is exited

I need to have a form where the save button is positioned outside of the form elements. However, when I move it elsewhere, the form does not refresh. <form ng-submit="save()" action="" name="addInv" novalidate> <div class="col-md-10 ...

Issue with three.js memory leak due to malfunctioning .dispose() method or incorrect usage

Greetings! I'm currently facing a memory handling issue. Despite researching various forums, I am still unable to identify the problem in my code. My project involves combining d3.js with three.js to create a visualization of nodes resembling planet ...

Having trouble removing date ranges from input-daterange in the bootstrap datepicker

Utilizing the input-daterange feature of the Bootstrap datepicker has been effective for my project so far. However, I have encountered an issue with clearing the dates and selection range within it. $(document).ready(function() { datespicker(); }); ...

Adding a close button inside a circular background using HTML/CSS

I'm struggling to align the X inside the circle perfectly; they just don't seem to match up no matter what I try. Here's a glimpse: https://i.sstatic.net/OIZik.png html: <div class="messages"> <span class="c ...

The use of jQuery.parseJSON is ineffective for a specific string

Why isn't jQuery.parseJSON working on this specific string? ({"stat":"OK","code":400,"data":[{"title":"Development Convention","event_type":false,"dates_and_times":[{"date":"28\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"dat ...

Executing an AJAX POST request from one domain (localhost:9393) to another domain (localhost:9696) using Spring Rest

I am encountering an issue with a form on one app running on localhost:9393. The JavaScript function used to post data is: function registerClient() { var postData = $("#client-reg").serializeArray(); var formURL = "http://localhost:9393/mPaws/client/re ...

Data obtained from the server includes JSON objects along with quotes and regular expressions

While analyzing the functionality of a search page server through element inspection, it was observed that requests are sent via POST with JSON parameters. To verify this observation, I recreated the same POST request using Insomnia with identical paramete ...

The initial rendering of components is not displayed by Vue Storybook

The functionality of the storybook is effective, but initially, it fails to "render" the component. By examining the screenshot, we can deduce that the component-template is being utilized in some way, as otherwise, the basic layout of the component would ...

Positioning elements in CSS using percentages allows for flexible and responsive

I am new to web design and I am trying to create a webpage similar to the one shown in this image. However, I want to set the height and width of the divs using percentage values instead of pixels. Whenever I attempt this, the boxes end up overlapping or ...

Is it mandatory for the npm install command to only be compatible with Node.js for the script

I've encountered an API that provides a JS SDK, and the instructions on its GitHub page suggest using npm install. My question is whether I need to have Node.js in order to use this SDK since it seems to be designed for it, or if I can simply work wit ...

Connect an angular directive template to an event

In my Angular directive, the template structure is as follows: <Button id="testBtn">Test me<button> Within the directive controller, I have defined API functions for the directive. Now, I need to send an event to the parent module when the bu ...

Having an issue with displaying the country name and country code in a table using the Angular7 custom pipe

country code: "ab", "aa", "fr", ... I need to create a custom pipe that will convert a countryCode into a countryName, such as: "ab" → "Abkhazian", "ch" → "Chinese", "fr" ...

What advantages does leveraging built-in functions offer when working with callbacks in JavaScript?

When it comes to array built-in functions, such as reduce and map, the main advantage is the ability to chain them together. For example, let's say you want to find the sum of elements in an array: const array = [1, 2, 3]; Aside from chaining method ...