Tips on excluding certain attributes in Vue 3?

I am looking to incorporate tailwind-merge into my Vue component to avoid conflicts with tailwind classes. An issue I've encountered is that when using useAttrs and adding a parent class, the additional classes are being duplicated.

The most straightforward solution is to utilize props, but I am curious if there is a way to apply the class attribute directly within the component itself.

<script setup lang="ts">
defineOptions({
  name: "FormInputLabel",
  // inheritAttrs: false, => this breaks the whole attribute injection
});

const { label = "", tag = "label" } = defineProps<{
  label?: string;
  tag?: "label" | "span";
}>();

const attrs = useAttrs();
const classAttrs = toRef(() => attrs.class);
</script>

<template>
  <component
    :is="tag"
    :class="twMerge(clsx('font-medium text-base', classAttrs))"
  >
    <slot>
      {{ label }}
    </slot>
  </component>
</template>

Answer №1

In Vue 3, you have the option to pass all attributes from a parent component to a child component using the v-bind="$attrs" feature.

<template>
  <component
    :is="tag"
    :class="twMerge(clsx('font-medium text-base', classAttrs))"
    v-bind="{ ...$attrs, class: undefined }"
  >
    <slot>
      {{ label }}
    </slot>
  </component>
</template>

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

Vue.js: Issue with dynamically calculating class property

I am attempting to create a computed class in vue.js 2.0 using the following syntax: <li :class="'str1' calcStarClass(1, p.rtg)"> </li> In my methods section, I have the foll ...

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({ ...

Can TypeScript be implemented within nuxt serverMiddleware?

I recently began diving into the world of nuxtjs. When setting up, I opted to use typescript. Initially, everything was running smoothly until I decided to incorporate express in the serverMiddleware. Utilizing the require statement to import express funct ...

Is there a way to display the true colors of a picture thumbnail when we click on it?

In my project, I attempted to create a dynamic color change effect when clicking on one of four different pictures. The images are displayed as thumbnails, and upon clicking on a thumbnail, it becomes active with the corresponding logo color, similar to t ...

Having trouble getting your Vue Tailwind project to start due to a postcss error?

I am experiencing an issue while trying to run my project on my laptop. The project works fine on all other devices with the same code, but for some reason, I am unable to run tailwind CSS on it. The error message I'm receiving is as follows: in ./sr ...

What is the method for adjusting the caret position in tinyMCE?

As part of my Vue component, I have implemented a custom editor using TinyMCE. However, I am experiencing an issue where after the @input event, the caret position is reset to 0 (the leftmost position). When attempting to retrieve the caret position, I us ...

When using Vue.js, the class binding feature may not function properly if it is referencing another data property that has variants

I am currently developing a basic TODO application. Within my index.html file, I have a main div with the id #app. Inside this div, there is another div with the class .todobox where I intend to display different tasks stored in my main.js file. Each task ...

The textarea fails to maintain the correct size when set to display none

Adjusting Textarea Style overflow:hidden; outline: none; border: none; width: 100%; resize: inherit; When running a foreach loop, notes are retrieved (newRecord) and the text and id of the original notes are extracted from them. To keep it ...

Keep an eye on the audio time changes in Vue!

I am working on a project where I have an audio file that plays songs. Currently, I am developing a slider that corresponds to the length and current time of the songs. However, I am facing a challenge as I cannot use player.currentTime in the watch compon ...

Updating default values in reactive() functions in Vue 3: A step-by-step guide

Currently, I am in the process of developing an application using Nuxt 3 and implementing the Composition API for handling async data. The specific scenario I am facing is this: I have a page that displays articles fetched from the database using useLazyFe ...

Navigating through Laravel's API

My software is quite small and I utilize both laravel and vue.js. I am curious about the variance between routing via api.php and web.php within the routes folder. Could someone please enlighten me on the distinctions between these two scenarios? ...

Tips for triggering a keyboard event to the parent in Vue 3

In my Vue 3 project, I have components nested five levels deep. The top-level component named TopCom and the bottom-level component called MostInnerCom both contain a @keydown event handler. If MostInnerCom is in focus and a key is pressed that it cannot ...

Vue.js: Issue with updating list in parent component when using child component

I'm encountering an issue when utilizing a child component, the list fails to update based on a prop that is passed into it. When there are changes in the comments array data, the list does not reflect those updates if it uses the child component < ...

Using Vue's V-IF directive to compare dates

On my website, I have an object that contains a field named available_at with the date in the format of 2019-08-08 I have a working HTML table utilizing Vue bindings but I am unsure how to compare the timestamp above using the built-in Date.now() method ...

Retaining filter values when using the Vue.js history back button

Recently, I've been given a project to work on that involves using Vue.js (I'm pretty new to Vue) The project includes creating a page that displays items based on filters set by the user. The user selects the filters and the listing page update ...

Failed Deployment on Netlify

My website is currently displaying an "Error establishing a database connection" message. The deploy log indicates that the issue lies within Gridsome. The log below details the deployment process. As a novice, I'm seeking advice on how to troubleshoo ...

Exploring the Possibilities of Nipplejs Integration in Vue with Quasar

Trying to implement Nipplejs in my Vue Project using quasar Components. Installed nipplejs through npm install nipplejs --save. Attempted integration of the nipple with the code snippet below: <template> <div id="joystick_zone">&l ...

When it comes to entering text in a text input or textarea within a large module in Vue.js, taking

While filling out my large form, I noticed a delay in rendering whenever I typed quickly into the input boxes. <b-form-input v-model="paymentItems.tierStepUPYear" type="text"></b-form-input> ...

How to Retrieve Information from an Array in VueJS

At the moment, the data being displayed on my page is an array, as shown in the snippet below: https://i.stack.imgur.com/zAvrc.png However, I only want to retrieve or display the project names. This is all I have at the moment: fetch(context,id){ ...

Is there an issue with VUE npm run serve due to Tailwindcss being absent?

A few months back, I embarked on a project using Vue and incorporated Tailwind CSS by running the command (Vue add Tailwind). Recently, when attempting to create a new project with the default Vue3 template using the command vue create project and then na ...