A guide on transferring CSS module generated classes to components in Vue

Looking to implement CSS Modules, however, it appears that vue does not have a built-in method to pass generated class names to child components.

Consider the scenario with two components:

  • Table.vue
  • TableRow.vue

With styles like this:

.table {
  table-layout: auto;

  &.hover .row:hover .cell {
    background-color: red;
  }
}

The styles generated for .row:hover no longer apply to TableRow, and I have yet to find a smooth way of passing it down to the child. The only solution I found involves passing the $style object as a prop to the child, which can get messy as the component tree grows larger since every component needs to include this prop and pass the $style object to subsequent children...

Answer №1

To apply a class to a component based on a variable, use v-bind.

<div :class="list_view === 'list' ? 'at-listview' : 'at-groupview'"></div>

In the example above, list_view is a variable.

If you need to pass these classes to another component, follow the same concept but this time pass the classes as a prop.

<child-component classes_to_send="class1 class2 class" />

Alternatively, you can bind a variable using a colon :.

<child-component :classes_to_send="class_from_variable" />

The classes can then be accessed in the props of <child-component/> as shown below:

<script>
   ...
   export default:{
      props:['classes_to_send'],
   }
   ...
</script>

<template>
   <div :class="classes_to_send"></div>
</template>

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

Navigate through FAQ items with sliders using Owl Carousel 2 - Easily switch between different chapters

Exploring the creation of a FAQ section with individual carousels for each item. My primary objective is for the carousel to transition to the next chapter when advancing beyond the last slide (backwards navigation would be a future enhancement). I'v ...

Error Message: "Attempting to access the 'on' property of an undefined object leading to an error."

While working on my web app and building the server part, I encountered a CMD error that says: TypeError: Cannot read property 'on' of undefined I have already attempted to establish the connection between server.js and client.js, but it s ...

Incorporating a personalized image to create custom icons on the Material UI bottom navigation bar

Is it possible to replace the default icon with a custom image in material ui's BottomNavigation component? I'm curious if Material UI supports this feature. If you'd like to take a closer look, here is the link to the codesandbox. ...

Set a dynamic Active Class on various divisions by utilizing their respective indexes

I have two divs with the same class. When I click on an anchor tag within one of the elements, I want to add a class to the corresponding position in the second div as well. Mirror: JSFiddle Here is the jQuery code snippet: $(document).on('click ...

Combining Vue.js with Laravel Blade

I've encountered an issue while trying to implement a Basic Vue script within my Laravel blade template. The error message I am getting reads: app.js:32753 [Vue warn]: Property or method "message" is not defined on the instance but referenc ...

Utilizing LessCss for color transparency and variable declarations with @vars

I'm attempting to create a function using LessCss, but I am encountering an issue: .transparent-border (@alpha:.15, @color:'0,0,0', @type: solid, @size:1px) { @val: @size @type rgba(@color, @alpha); border: @val; } The problem er ...

"Ensuring a DIV element has a height of 100% within

Is there a way to set and enforce a div's height to be 100% of the browser window, another div, or a table cell without resorting to complicated hacks found on Stack Overflow or other websites? ...

Put the code inside a function. I'm new to this

My goal is to encapsulate this code: if($(window).width() > 980) { $(window).on("scroll", function() { if($(window).scrollTop() > 20) { //add black background $(".x-navbar").addClass("active"); $(".x-navbar .desktop ...

The Jquery tooltip does not correctly track the mouse movement across all div elements

Previously, I encountered an issue with the Jquery Mousemove feature not working properly on tooltips within divs in a 14 column 960 grid system. Now, although they work to some extent, they only follow my mouse up to the 7th or 8th column width. When scro ...

What is the process for implementing JavaScript in Django?

I'm a newcomer to this and although I've tried searching on Google, I haven't found a solution. I'm facing an issue where I can include JavaScript within the HTML file, but it doesn't work when I try to separate it into its own fil ...

My goal is to dynamically assign a class to an iframe element by identifying a specific class contained within the html of the iframe

I need assistance with adding a class to an iframe only if the body tag has a specific class name. Here is my attempt at writing some code: <iframe class='iframeContent' src='' width='100%' height='' frameborder= ...

I'm feeling lost on how to implement a simple class pattern using object-oriented JavaScript

I've been struggling with OO Javascript and can't even recognize the same question that has been asked before. My goal is to write a Javascript class that can execute the common pattern shown below: var User=require('./models').User() ...

Stopping HTTP response in middleware using node.js and express - is it possible?

Currently, I am working on developing a timeout middleware using express in node.js. app.use((req, res, next) => { res.setTimeout(3000, () => { console.warn("We've reached the timeout limit - ending response with status code 408" ...

Incorporate characteristics into a text box using jQuery

Currently, I am working with asp.net MVC. There is a control that looks like this: <%= Html.TextBox("username") %> I would like to add a lost focus event to this control. This is what I have tried: $(document).ready(function() { $("#username") ...

The system encountered an issue with the CSS code, indicating "Invalid CSS after "...inear-gradient(": expected selector, was "{"

While attempting to compile my sass code, I encountered the following error message regarding a syntax issue: /* { "status": 1, "file": "C:/Users/faido/Desktop/Productivity/sass css/project/sass/main.sass", "line": 36, "column": 9, "mes ...

Transforming class components to utilize hooks (storing state within an if statement)

I am facing a challenge with some old code that is structured within a class and I need to convert it to hooks. I have successfully converted most of the code, except for one instance where state is declared inside an if statement. The other conversions in ...

When attempting to retry in Next.js, the props obtained from getServerSideProps may

Currently, I am facing an issue with sending axios requests in getServerSideProps and passing the value through props via SSR to the client. The challenge lies in including a token in the header using an instance. In case the token expires, I utilize refre ...

What methods can be used to supersede CSS styles?

Illustrated in the example below http://jsfiddle.net/xBRPg/ My objective is to establish a yellow backdrop for a specific row in my table. However, when I utilize the code snippet below <table class="table table-bordered table-striped"> The row&a ...

Utilize inline scripts within the views of Yii2 for enhanced functionality

I stumbled upon a jQuery code online that allows for the integration of Google Maps, and I'm looking to implement it in my application to ensure accurate address retrieval. You can find the jQuery code here. Currently, I am working with yii2 Advanced ...

Creating dynamic values in data-tables using Vuetify

As I work with JSON data, my current task involves formatting it using Vuetify's Data Tables. The official documentation provides guidance on defining table headers as shown below: import data from './data.json' export default { data ...