Enhance your VueJS and Tailwind development with dynamic classes and variables

I'm attempting to incorporate dynamic variables (props) into a Tailwind CSS class, but I seem to be encountering an issue:

:class="`w-${percent}/12: ${show}`"

Upon running this code, the output is as follows:

<div class="w-0 h-2 transition-all duration-1000 ease-out bg-indigo-600 rounded-lg w-11/12: true"></div>

I am puzzled by why ':true' is being added.

Thank you for any assistance provided.

Note:

Answer №1

The reason for this behavior is that you are utilizing template literals, which return a string. Consequently, the show variable is evaluated as boolean true and it displays as "true".

If you desire to switch classes based on the state of the show variable, you should opt for the object approach.

<div
  class="w-0 h-2 transition-all duration-1000 ease-out bg-indigo-600 rounded-lg"
  :class="{ [`w-${percent}/12`]: show }"
>
  YOUR CONTENT
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with VueJS: Method not displaying returned data

I am successfully able to retrieve data in the console. However, I am encountering an issue when trying to display this data on the webpage using double curly braces. The rest of the template displays fine without any issues. Template: <template> ...

What could be the reason for the Laravel middleware CORS not functioning properly in conjunction with Vue

Currently, I am working with Laravel 5.7 as the backend and Vue.js as the frontend. To send HTTP requests from Vue to the Laravel backend, I decided to use the Laravel CORS package, which can be found at this GitHub link: https://github.com/barryvdh/larave ...

Ways to display a sorting icon depending on a calculated attribute

My goal is to display the appropriate sorting icon based on a computed value. For instance, if the column1 is clicked, one of two classes, classRefcodeDown or classRefcodeUp, should be true. Both computed properties invoke a method called sortClassRefcode, ...

Vue draggable component

I'm currently facing an issue while trying to integrate the Vue Draggable Component into my existing component. Everything works perfectly fine in my component without the draggable feature, but as soon as I add it, the entire content becomes blank. I ...

Using Vue.js to manage connected data for a multi-select dropdown

Here is the code snippet: <li> <select v-model="selectYear" id="search-year" class="w-100 text-center mb-1" name="year" > <option v-for="year in years" v-bind:value="year.value"> {{ year.text }} </optio ...

Troubleshooting: Issue with v-link to classname in Vue.js

This is the code I am working with: <div v-link="'/work/' + {{data.useClass}}" class="itemImg {{data.useClass}}"> When rendered in the browser, it looks like this: <div class="itemImg x50"> The expectation is that class="itemImg { ...

Guide on implementing enums (or const) in VueJS

Seeking help on a seemingly simple task, I am trying to understand how to use "enums" in VueJS. In my file named LandingPage.js, I have the following code: const Form = { LOGIN: 0, SIGN_UP: 1, FORGOT_PASSWORD: 2, }; function main() { new Vue({ ...

Using Vue along with bootstrap-vue: Ensuring only one list element is expanded in a list (v-for) at any given time

In my Vue project with bootstrap-vue, I am utilizing the b-collapse feature within a v-for loop for lists. While it is functioning correctly, I am struggling to automatically close expanded list elements when a new one is clicked. Here is my question: Ho ...

Guide on plotting latitude and longitude coordinates on Google Maps with Vue.js through JSON data fetched via AJAX

I have implemented a Vue.js script to fetch JSON data through an AJAX request. However, I am encountering issues with the code. <script> new Vue({ el: '#feed' , data: { details: [], }, mounted() { this.$nextTick(fu ...

The vuex module has replaced the state field "foo" with another field of the same name located at "foo"

There is a warning showing up in the console that says: [vuex] state field "foo" was replaced by another module with the same name at "foo" Can you explain what this warning indicates and how I may have made an error? ...

I am utilizing Vuex to store all of the product details and handle API request queries efficiently

Question regarding Vue/Vuex efficiency and best practices. I am working with an API that returns a paginated data-set of 50 products, which I am storing in a Vuex store. When displaying all products, I iterate over the Vuex store. Now, for individual pr ...

Arranging an array based on relationships between children and parents

I have an array of objects like this: const data = [{id: 3, name: ''}, {id: 4, name: ''}, {id: 5, name: '', parent: 3}, {id: 6, name: '', parent: 5}, {id: 7, name: '', parent: 4}, {id: 8, name: '&ap ...

What is the best way to initiate a change when adding a new property to an object in VueJs?

Here is the initial structure of my vuex state in a VueJS project: state: { level1prop: null } After dynamically changing and mutating it, the structure becomes: state: { level1prop: { level2prop: { level3prop: { ...

Instructions for showcasing a 404 error page in the event that a back-end GET request to an API fails due to the absence of a user. This guide will detail the process of separating the

I am currently working on an application that combines JavaScript with Vue.js on the front-end and PHP with Laravel on the back-end. When a GET request is made from the front-end to the back-end at URL /getSummoner/{summonerName}, another GET request is t ...

Struggling to set up Tailwind in NuxtJS configuration

Using the "@nuxtjs/tailwindcss": "^2.0.0" for my Nuxt App has been quite helpful. Upon installation, it generated a tailwind.config.js file. I made some modifications to the code below: module.exports = { theme: {}, variants: {}, plug ...

Error in Vue.js: When using v-for, it is not possible to read a property

I'm working on creating multiple FAQ boxes using a list of objects containing questions and answers. However, I keep encountering the error message "Cannot read property 'question' of undefined." What could be causing this issue? Here's ...

Harness the power of Vue.js by implementing plugin methods in your code

For my first attempt at building a SPA with Vue, I decided to re-use a few functions but encountered some issues. The error message "this.ExperienceToLevel is not a function" kept popping up and it left me puzzled. Furthermore, I'm contemplating if c ...

Displaying content conditionally - reveal a div based on user selection

I have a dropdown menu and need to determine whether to display a specific div using the value selected via v-if. <select class="custom-select custom-select-sm" id="lang"> <option value="1">English</option> <option value="2">Sv ...

Error: The use of "let" as a lexically bound identifier is not permitted

Currently working with vue.js and encountering the error message "Uncaught SyntaxError: let is disallowed as a lexically bound name". When trying to troubleshoot, I'm faced with a blank screen and this error appearing in the console. I've search ...

The software, Tailwind, encounters an error stating "Error: PostCSS plugin postcss-flexbugs-fixes necessitates PostCSS 8

I encountered an error message while trying to install Tailwind for my React dictionary application: These are the problems that were encountered during compilation: ERROR in ./src/App.js 4:0-19 The module './App.css' could not be found in &ap ...