Style within Vue.component

0

The question is quite simple: Can I insert CSS / SCSS style within a Vue.component () , to stay within the scope of the component?

Something like

const Color = Vue.component('Color', {
    props: ['row'],
    template: '<div class="badge-color" :style="{backgroundColor: this.row.color}"></div>'

    //parte abaixo é sugestiva, apenas para ilustrar o exemplo
    style:{'.badge-color':'width:100%'}
})

The above scenario is a "mini" component within another component, for organization only. I do not want to create a separate .vue file for this as it is very simple - and necessary due to the use of the dataTable Vuejs.

    
asked by anonymous 09.01.2018 / 12:38

2 answers

1

This is not possible outside single file components , as the instance of components in mode traditional does not provide the option of style CSS native, but the question is, should you provide?

I do not think so as this encompasses the division of tasks between languages, by the way this theme is being strongly discussed, understand .

So this can only be done by means of libraries or by using: style individually in which you can encapsulate a CSS object for each component.

    
12.01.2018 / 13:32
0

Updated response:

If I understand correctly, one way to solve is to create the Color component within the "parent" component, ie to not create a new / component in the case of the Color component. In tag style you normally put the classes as if they were for the "Father" component, but they are only used in the "Son" component, for example:

Parent component

<template lang="pug"> 
  div
    color(:row="{ color: 'red' }")
</template>
<script>
  import Vue from 'vue'

  Vue.component('color', {
    props: ['row'],
    template: '<div class="badge-color" :style="{backgroundColor: this.row.color}"><p>teste</p></div>'
  })
</script>

<style lang="stylus">    
  .badge-color
    width 100%
</style>

Another way to do this is to put Global% in your main.js file or other configuration file, for example:

main.js

import Vue from 'vue'
import App from '@/App'
import router from '@/router'
import store from '@/store'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
}) 

Vue.component('color', {
  props: ['row'],
  template: '<div class="badge-color" :style="{backgroundColor: this.row.color}"><p>teste</p></div>'
})

And in your component you want to use the Color component, you normally call it, and it inserts css into your tag style in the same way, p>

Parent component using the Global Color component

<template lang="pug"> 
  div
    color(:row="{ color: 'red' }")
</template>
<script>      
// código
</script>

<style lang="stylus">    
  .badge-color
    width 100%
</style>
    
09.01.2018 / 14:21