I would like to dynamically update styles inside
style tags.
However creating a container model for Vue, removes the style
tags.
I know sty
Here's what I think is a good workaround/solution.
It is just a custom component, so it's as reusable as it gets. All of Vue's goods like v-if
can all be used.
Another pro is that the styles generated will be there only as long as the component is!
Vue.component('v-style', {
render: function (createElement) {
return createElement('style', this.$slots.default)
}
});
// demo usage, check the template
new Vue({
el: '#app',
data: {
bgColor: 'red'
}
})
.stuff input {
background: {{bgColor}};
}
Remove "red" and type "yellow":
The one drawback I see is that since the name of the tag is
(or whatever you chose to call it) and not , the IDEs may not color it nicely. But otherwise it'll just be like a regular
tag.
v-bind:style
This doesn't modify style
tags, but the standard way of setting styles is using object style bindings.
Basically you'd use a :style
attribute and assign to it the CSS properties of the style in the form of an object. Demo below.
new Vue({
el: '.setting',
data: {
bgColor: 'red'
},
computed: {
inputStyles() {
return {
background: this.bgColor
}
}
}
});
Remove "red" and type "yellow":