I've been encountering a recurring issue where I need to dynamically set the background of different elements within my component as linear gradients. Specifically, I'm working on creating a grid background that adjusts and scales based on various inputs.
What I'm aiming to do is construct my CSS linear-gradient
background property in a function. Here's a simplified example of what I'm attempting:
createBackgroundString(){
return 'linear-gradient(' + this.angle + 'deg, ' + this.color1 + ', ' + this.color2 + ')';
}
Once I have this string, my goal is to insert it into the :style
attribute so that Vue can apply it dynamically:
v-bind:style="{ background: createBackgroundString() }"
Unfortunately, Vue doesn't accept this approach and simply ignores it. It seems that the resulting string is too complex for the template property which expects simple strings like 'red' or '#FF0000'.
Is there any clever workaround or hack to achieve this functionality in Vue? Currently, I'm forced to fall back on using jQuery for this task, which is less than ideal.