I am trying to dynamically style an element based on a pokemon's type (eg. fire gets a fire icon, fire/flying gets 1 fire 1 flying).
I tried to do it with a ternary operator and :style but it got really long and messy, so I'd prefer not to. Currently, I have it set as a method, and pass in an array, which look like this:
types: ["water", "flying"] //or sometimes just one value eg: types: ['fire']
here is my method:
methods: {
typeStyle: function (types) {
const backgroundImageUrls = []
for (const i in types) {
backgroundImageUrls.push('url(../assets/' + types[i] + '.svg)')
}
console.log(backgroundImageUrls)
let backgroundPosition = 'center'
if (backgroundImageUrls.length > 1) {
backgroundPosition = 'left right'
}
return {
backgroundImage: backgroundImageUrls.join(','),
backgroundPosition
}
}
}
and this is the html template it is called in:
<li
class="card"
v-for="(mon, index) in team"
:key="index"
>
<div class="cardfront-images"
:style="typeStyle(mon.types)"
>
...
</li>
but it's not working. I would also like to apply a 2nd effect, background-position and do background-position: "center" if there is 1 type, and background-position: "left right" if there are two. but I get an error because of the hyphen in the CSS property.
EDIT
So I have it working to where it makes a url() for the background image (yay!), but unfortunately styling is not applied. So something isn't working, obviously. I have also updated my codeblocks to reflect changes.
EDIT2: So this solution did work, and I have given the check. For some reason, it didn't like my local assets being string-literaled in, so I just called the images from my git repo. Which I guess is good enough since I'm really just making this for my own education.