Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
vue3 composition api adds or removes classes for custom input boxes
<script lang="ts" setup>
import { ref, computed } from 'vue'
const { modelValue = '' } = defineProps<{
  modelValue: string
}>()
const emit = defineEmits(['update:modelValue'])

const isFilled = ref(false)

const value = computed({
  get() {
    return modelValue
  },
  set(value: string) {
    isFilled.value = value.length > 0
    emit('update:modelValue', value)
  }
})
</script>

<template>
  <input v-model="value" :class="{ 'filled': isFilled }" v-bind="$attrs" />
</template>

sfc playground

This will not take effect when the key is pressed for the first time. For example: press abcdefg, the last input box displays bcdefg, delete it with the backspace key and try again, it is still the same.

I commented out isFilled.value = value.length > 0 and it works fine, but otherwise, how can I add a class to the element?

The effect I need is to add a class named filled to it when the value of the input box is not empty.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would have used a watcher for this task, example code:

<script lang="ts" setup>
import { ref, watch } from 'vue'

const { modelValue = '' } = defineProps<{
  modelValue: string
}>()
const emit = defineEmits(['update:modelValue'])
const isFilled = ref(false)
const inputText = ref("")

watch(inputText,(newValue, oldValue)=>{
  isFilled.value = inputText.value.length > 0
  emit('update:modelValue', inputText.value)
})
</script>

<template>
  <input v-model="inputText" :class="{ 'filled': isFilled }" v-bind="$attrs" />
</template>

If you just want to add the class you can do that inline aswell. example:

<script lang="ts" setup>
import { ref, watch } from 'vue'
const inputText = ref("")
</script>
<template>
  <input v-model="inputText" :class="{ 'filled': (inputText.length>0) }" v-bind="$attrs" />
</template>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda