0

A plugin I use creates dynamic html and I want to add a dynamic background-color using a hex passed via props.

This is the html in my component

<template>
  <div class="pagination" slot="pagination"></div>
</template>

Generates dynamic HTML of this

<div class="pagination" slot="pagination">
  <span class="swiper-pagination-bullet"></span>
  <span class="swiper-pagination-bullet"></span>
</div>

The components receives props

props: ['primaryBgColor']

I can obviously see the color in the template if I write

<template>
  <div>
    {{ this.primaryBgColor }}
  </div>
</template>

However when I write a style within the component like

<style>
  .swiper-pagination-bullet {
    background-color: {{ this.primaryBgColor }}
  }
</style>

Webpack returns an error saying I have a CSS syntax error. Any ideas?

4
  • I haven't seen any framework which would support dynamically generating CSS classes based on JS. This doesn't even sound like a good practice. Simply just apply inline styles in my opinion which are completely valid in context of such frameworks, especially for such dynamic coloring stuff. Commented Sep 11, 2017 at 5:08
  • I would add inline style, but the HTML doesn't exist in the template file because it is injected by the plugin. Commented Sep 11, 2017 at 5:10
  • "I would love to write a style within the component but I haven't been able to get that to work". What doesn't work about it? If you can write {{ this.primaryBgColor }} and have it output your color, why should it break when you wrap it in a class name inside style tags? Commented Sep 11, 2017 at 5:45
  • @EdmundReed webpack cannot interpolate the variable. It says my CSS contains a syntax error. Commented Sep 11, 2017 at 5:58

2 Answers 2

1

suresh's answer may not work as it listens to the Vue mounted event, but by the time Vue component mounted, the plugin element may not yet be been injected.

if the plugin provides a similar event, you can register the handler there. if not, you can listen to the outer dom element using MutationObserver instead.

<template>
  <div id="outer">
  </div>
</template>
<script>
    const observer = new MutationObserver(mutations => {
       // suresh's function here
    });
    observer.observe(document.getElementById('outer'), { childList: true });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

In your template, you can directly inject style

<template>
  <div :style="this.primaryBgColor">
    {{ this.primaryBgColor }}
  </div>
</template>

primaryBgColor should contain object like {'background':"#cccc"}

For more option, vuejs had superb documentation. you can refer https://v2.vuejs.org/v2/guide/class-and-style.html#Object-Syntax-1

We can query the element and apply style like as follows

mounted: function () {
      var elems = document.querySelectorAll('.swiper-pagination-bullet ')
      var index = 0
      var length = elems.length
      for (; index < length; index++) {
        elems[index].style.backgroundColor = this.primaryBgColor
      }
    },

4 Comments

Thanks for your answer, however, I need the CSS applied to the span elements, not the div.
And I do not have access to the span elements within my template file because they are injected by a plugin.
Updated my answer, check it
Awesome, I will test it out first thing tomorrow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.