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

246
Vistas
En Vue v2, ¿cómo verifico si un valor ingresado en un formulario está vacío?

He intentado muchas formas diferentes de hacer esto con alerta () y declaraciones simples if-else, e incluso lo que está en este enlace: https://vuejs.org/v2/cookbook/form-validation.html

¡Pero nada funciona! ¿Qué estoy haciendo mal?

¿Qué hay en mi index.html:

 <div id="app"> <div class = "addTask"> <h1>A List of Tasks</h1> <p v-show="activeItems.length === 0">You are done with all your tasks! Celebrate!</p> <form @submit.prevent="addItem"> <input type="text" v-model="title"> <button type="submit">+</button> </form> </div>

Esto es lo que tengo en scripts.js:

 var app = new Vue({ el: '#app', data () { return { // errors: [], items: [{ userId: 0, id: 0, title: "", completed: false, }], title: '', show: 'all', } }, // Using axios to asynchrously query the API mounted () { axios .get("https://jsonplaceholder.typicode.com/todos") .then(response => this.items = response.data) }, computed: { activeItems() { this.saveItems; return this.items.filter(item => { return !item.completed; }); }, filteredItems() { // An active state denotes the task is not yet completed if (this.show === 'active') return this.items.filter(item => { return !item.completed; }); if (this.show === 'completed') return this.items.filter(item => { return item.completed; }); // This makes it so added tasks go to the top, not bottom return this.items.reverse(); }, }, methods: { addItem() { if(this.items != 0) { this.items.push({ title: this.title, completed: false }) this.title = ""; } else { alert("Please enter a task.") }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Según su código, debe declarar una propiedad llamada title en la función de datos y verificar si el title está vacío o no en el método addItem .

 var app = new Vue({ el: '#app', data () { return { // errors: [], title: '', // add this property in data function items: [{ userId: 0, id: 0, title: "", completed: false, }], title: '', show: 'all', } }, // Using axios to asynchrously query the API mounted () { axios .get("https://jsonplaceholder.typicode.com/todos") .then(response => this.items = response.data) }, computed: { activeItems() { this.saveItems; return this.items.filter(item => { return !item.completed; }); }, filteredItems() { // An active state denotes the task is not yet completed if (this.show === 'active') return this.items.filter(item => { return !item.completed; }); if (this.show === 'completed') return this.items.filter(item => { return item.completed; }); // This makes it so added tasks go to the top, not bottom return this.items.reverse(); }, }, methods: { addItem() { if(this.title !== '') { //check if `title` is empty or not this.items.push({ title: this.title, completed: false }) this.title = ""; } else { alert("Please enter a task.") }

Creé un ejemplo en Stackblitz . También puede ver el ejemplo directamente haciendo clic aquí

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