How to define css styles for a vue.js component when registering that component? How to define css styles for a vue.js component when registering that component? vue.js vue.js

How to define css styles for a vue.js component when registering that component?


there does not seem to be a css option.

That is correct. You cannot do what you describe. The documentation on single file components is pretty clear that one of the advantages is that you can do this with them and cannot do it without them.

In many Vue projects, global components will be defined using Vue.component, followed by new Vue({ el: '#container' }) to target a container element in the body of every page.

This can work very well for small to medium-sized projects, where JavaScript is only used to enhance certain views. In more complex projects however, or when your frontend is entirely driven by JavaScript, these disadvantages become apparent:

[...] No CSS support means that while HTML and JavaScript are modularized into components, CSS is conspicuously left out


Its true that you cannot add <style> inside a Vue template or add CSS withincomponent directly, unless you bind it or define your css globally. But you can create a custom component that will dynamically do it for you. sample


Here is an answer all the way from Pakistan :)

export const ContactUs = Vue.component('mycomponent-contact-us',{    props: {        backgroundColor: {            type: String            ,required: false            ,default: "red"        }    }    ,data: function(){        return {        }    }    ,template: `        <div>            <span class='contact_us_text' >Contact Us Component and its bg color will be {{backgroundColor}}</span>        </div>    `    ,mounted: function(){        var css_text = `        .contact_us_text{            color: `+this.backgroundColor+`;        }        `;        var css = document.createElement('style');        css.type='text/css';        css.setAttributeNode( document.createAttribute('scopped') );        css.appendChild(    document.createTextNode( css_text )     );        this.$el.appendChild(   css     );    }}

);