I'm working with a basic Vue.js component where I'm rendering a snippet of HTML:
...
<div class="sml-button" v-on:click="toggleButton()" v-html="button"></div>
...
When the button is clicked, the toggleButton()
function updates the button
data:
toggleButton: function() {
if (this.shouldBeShown) {
this.button = 'show less';
} else {
this.button = '<span>...</span>show more';
}
}
Take note of the <span></span>
element in the else
condition.
The issue I'm facing is that I can't style this dynamically created span
element within my component:
<style lang="sass" scoped>
.sml-button {
color: #4390ca;
font-size: 14px;
font-family: latoregular, Helvetica, Arial, sans-serif;
span {
color: #3f3f3f; /* THIS WON'T BE COMPILED */
}
}
</style>
The parent class (.sml-button
) has its styles, while the child span
does not.
How can I apply styles to a dynamically added HTML element inside a Vue.js component?