Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Variables in Vue Nuxt Tailwind

This is my loop

<template>
    <div>
        <!-- title -->
        <section
            class="pt-4 w-fit mx-32 grid grid-cols-1 lg:grid-cols-4 md:grid-cols-2 justify-items-center justify-center gap-y-20 gap-x-20 mt-4 mb-5">

            <tr v-for="product in products" :key="product.id">
                <div class="w-56 bg-white rounded-xl duration-500 hover:scale-105 hover:shadow-xl">
                    <div v-for="(image, index) in product.images" :key="image.url">
                        <div v-if="index === 0">
                            <img :src="image.url" alt="" class="h-[14rem] w-72 object-cover rounded-xl">
                        </div>
                    </div>
                    <div class="px-2 py-3 w-60">
                        <p class="text-lg font-semibold text-black truncate block capitalize">{{ product.name }}</p>
                    </div>
                    <stars :numStars=product.stars :numReviews=product._count.reviews></stars>
                    <div class="flex items-center">
                        <p class="pl-2 text-lg font-bold text-black cursor-auto my-1">R$ {{ product.price }}</p>
                        <div v-for="(sticker, index) in product.stickers" :key="index">
                            <p>Sticker name: {{ sticker.name }}</p>
                            <p>Sticker discount: {{ sticker.discount }}</p>
                            <div :title=sticker.color></div>
                            <p class="">Sticker color: {{ sticker.color }}</p>
                            <p>Sticker text color: {{ sticker.textColor }}</p>
                        </div>

                    </div>

                </div>


            </tr>


        </section>
    </div>
</template>

im getting data from a api, that returns this in the browse (only the last product has sticker in this case, and i put random colors just to test):

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how could i use sticker-color and text-color to use then dynamically, every product can have two stickers so it can be hard to use the colors, but even if someone knows how to do this with one sticker i will be very grateful, below is the layout im trying to reach:

enter image description here

>Solution :

Consider foregoing Tailwind altogether in this situation and instead use a inline style attribute to apply the dynamically-fetched colors:

<div :style="{ color: sticker.textColor, backgroundColor: sticker.color }">

This is because Tailwind needs to see classes as full unbroken strings at build time in source code but that is not possible if you are getting values dynamically from some other source at runtime.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading