Discover the wonders of utilizing @blur events on your custom Vue components!

Trying to create a customized component that mimics an input field with validation, I'm encountering issues with getting @Change, @blur, and other events to function properly as they would on a standard input field.

This is the structure of my custom component:

// custom-input.html

<div>
  <multiselect
    v-if="hasOptions"
    v-model="selected"
    :options="optionsList"
    :name="name"
    :searchable="false"
    :allow-empty="false"
    :maxHeight="1000"
    track-by="date"
    label="label"
    placeholder="Please select"
    key="select"
  >
  </multiselect>
  <input
    v-if="!hasOptions"
    type="text"
    pattern="[0-9]*"
    inputmode="numeric"
    v-model="selected"
    v-input-mask="'date'"
    v-suppress-aria-invalid
    key="input"
    :name="name"
  />
</div>

Below is an example of how the component is used within another component:

// parent-form.component.html

<custom-input
      :initial="exampleDay"
      :name="'exampleDay'"
      v-model="exampleDay"
      @blur="v$.exampleDay.$touch"
      @change="v$.exampleDay.$touch">
</custom-input>

The error message received is:

Component emitted event "blur" but it is neither declared in the emits option nor as an "onBlur" prop.

Answer №1

When working with a custom component, it is important to manually define the behavior of the @blur event by binding events to components through emits.

To do this, you simply need to specify that the @blur event will emit another event within the component code:

export default {
    ..
    ..
    emits: ['event_name'],
    methods: {
        blurHandler() {
            this.$emit('event_name')
        },
    },

};

Then, in the parent component, you can call this custom event and define its functionality as needed.

I hope this explanation was clear and helpful for you!

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

Puzzle: Ensuring all properties of JavaScript objects within an array are defined

Consider the vue.js object provided below: FormattedData: Object 1: Object approved: true 2: Object approved: undefined 3: Object approved: false 4: Object approved: true Seeking a more effective and concis ...

Exploring the optimal method to seamlessly blend Node.js and Vue.js together

Can anyone share some recommendations on the best practices for combining Node.js and Vue.js in applications? I've been exploring different options, but I'm still unsure of the most optimal approach. ...

vee-validate's implementation of useFieldArray is specifically designed to function exclusively with object deconstruction

Greetings everyone, Could someone please shed some light on why vee-validates useFieldArray only functions correctly when I destructure it? Here is the template: // In the functioning example, I utilize "fields" instead of fieldArray.fields. ...

Utilizing images in a compiler run with the option of `allowJS:true` is key

Looking to develop a React Native library using JavaScript instead of typescript, but want to leverage TSC for its ability to generate type declarations from jsdoc comments. However, encountering an issue where local images are not included when the ts com ...

Component coding in Angular 2 allows for seamless integration and customization of Material

I am looking to initiate the start.toggle() function (associated with Angular 2 material md-sidenav-layout component) when the test() method is triggered. How can I execute md-sidenav-layout's start.toggle() in the app.component.ts file? app.componen ...

Filtering a key-value pair from an array of objects using Typescript

I am working with an array of objects containing elements such as position, name, and weight. const elements = [{ position: 3, name: "Lithium", weight: 6.941, ... },{ position: 5, name: "Boron", weight: 10.811, ... }, { position: 6, name: "Carbon", weight: ...

Problem with selecting dates in rangepicker

Having trouble with my recursion code for selecting dates in a rangepicker: recurse( () => cy.get('.mantine-DatePicker-yearsListCell').invoke('text'), (n) => { if (!n.includes(year)) { //if year not f ...

Futuristic routing in VueJS

I've been attempting to dynamically generate routes, but unfortunately I'm facing difficulties as it seems not feasible with Vuejs 3 import { createRouter, createWebHistory } from "vue-router"; import TopMenu from "../layouts/top-m ...

Exploring the possibilities of socket.io-client in combination with Vite.js and Vue for seamless real

I am currently diving into socket.io for my upcoming Vue project, but I seem to be encountering some issues. Interestingly, everything works smoothly when I use vue-cli, however, I prefer working with Vite.js due to its speed and customization options. Unf ...

Verify if an item is present within a separate array

To determine if an object in one array exists in another array, we can use the combination.some() method with a condition that checks for a match based on specific criteria. In the example below, the event array returns true while the event1 array return ...

Utilize JavaScript libraries in a TypeScript project

Looking to integrate a payment system called iyzico into my project. The iyzico payment library can be found here. However, there are no types available for it. How can I utilize this library in my Node.js TypeScript Express backend project? I attempted t ...

Beginner's Guide: Building your debut JavaScript/TypeScript library on GitHub and npm

I am looking to develop a simple JavaScript/TypeScript library focused on color conversion. Some of the functions and types I aim to export include: export type HEX = string; export type RGB = { r: number; g: number; b: number }; export type RGBA = { r: n ...

What causes Enum[Enum.member] to be undefined in the TypeScript playground on codepen.io?

My intention was to test out some type settings on TypeScript playground at codepen.io, but I encountered an unexpected issue: enum Order { Asc = 'asc', Desc = 'desc' } console.log(Order[Order.Asc]); // undefined in codepen.io ...

What could be causing Typescript Intellisense to not display Object extensions?

Let's take a look at this unique way to extend the Object type: interface Object { doSomething() : void; } Object.prototype.doSomething = function () { //perform some action here } With this modification, both of the following lines will c ...

Managing image src links in VueJS for both development and production modes

Struggling with loading images on my Vue.js app. Here's what I have: <img :src="imgSrc" /> It works fine in development mode: data() { return { imgSrc: require('../assets/MyLogo.png'), } } And it also works in production m ...

Pass multiple variables from a Laravel controller to a Vue component

Let's consider a Vue component as shown below: export default { created() { axios.get("a url").then(res => { console.log(res.data); }); } }; Next, Axios sends a request to the following function in a Laravel co ...

Modify color of chosen item using button event in material ui list

My sidebar contains buttons, and when I click on a button, I want to change its color to indicate it’s selected. However, the color change doesn't always work as expected, sometimes requiring two clicks for it to take effect. Additionally, despite u ...

Ensure that the output of a function in Typescript matches the specified data type

Looking for a way to inform TypeScript that the output of a function will always meet the type requirements of a variable, therefore avoiding any error messages? Type 'string | Date' is not assignable to type? For example: const getStringOrDat ...

Custom element not found: Have you properly registered the component? When using recursive components, be sure to include the "name" option

The code is functioning correctly in the development environment, but when it is pushed to production, I encounter an error. Although I have added the name attribute to the component, it still does not work. As a temporary solution, I removed the spinner c ...

Unable to make a reference to this in TypeScript

In my Angular2 application, I have a file upload feature that sends files as byte arrays to a web service. To create the byte array, I am using a FileReader with an onload event. However, I am encountering an issue where I cannot reference my uploadService ...