Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vue.js component, how to use props in css?

I'm new to vue.js. Here is my problem:

In a *.vue file like this:

<template>   <div id="a">   </div> </template>  <script>   export default {     name: 'SquareButton',     props: ['color']   } </script>  <style scoped>     #a {       background-color: ?     } <style> 

How can I use the props color in background-color: (where is a ? now).

Thanks.

like image 207
MingWen Avatar asked Mar 18 '17 08:03

MingWen


People also ask

Can you use props in CSS?

Any component or element that accepts a className prop can also use the css prop. The styles supplied to the css prop are evaluated and the computed class name is applied to the className prop.

How do you pass props to Vue component?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

How do you write CSS in Vue?

In Vue. js, we can add an inline style to our element by binding the style attribute in the HTML tag. For reference, :style is shorthand for v-bind:style . Inline styling can be done in two ways: using object syntax or array syntax.

How do props work in Vue?

What are props? In Vue, props (or properties), are the way that we pass data from a parent component down to it's child components. When we build our applications out of components, we end up building a data structure called a tree.


1 Answers

You actually can!

You should define the CSS variables in a Computed Property, then call the computed property as a style attribute to the element that will require the CSS variable, and finally you may use the variable within the tags at the bottom of your document.

new Vue({    el: '#app',    data: function() {      return {        baseFontSize: 1,        bgHoverColor: "#00cc00",        hoverContent: "Hovering!"      }    },    computed: {      cssProps() {        return {          '--hover-font-size': (this.baseFontSize * 2) + "em",          '--bg-hover-color': this.bgHoverColor,          '--hover-content': JSON.stringify(this.hoverContent)        }      }    }  })
div {    margin: 1em;  }    div.test:hover {    background-color: var(--bg-hover-color);    font-size: var(--hover-font-size);  }    div.test:hover::after {    margin-left: 1em;    content: var(--hover-content);  }
<script src="https://unpkg.com/vue/dist/vue.js"></script>    <div id="app" :style="cssProps">      <div>Hover text: <input type="text" v-model="hoverContent"></div>    <div>Hover color: <input type="color" v-model="bgHoverColor"></div>      <div class="test">Hover over me</div>  </div>

Or have a look here: https://codepen.io/richardtallent/pen/yvpERW/
And here: https://github.com/vuejs/vue/issues/7346

like image 180
Yuri M Avatar answered Sep 18 '22 17:09

Yuri M