What is the best way to transfer a value to v-model using emit?

I'm having trouble passing the selected value from a select field to v-model. Currently, the code only seems to work with a v-text-field.

<script>
    export default {
        name: 'FormSelect',
        props: {
            modelValue: {
                type: [String, Number],
                default: '',
            },
            label: {
                type: String,
                default: '',
            },
            items: {
                type: Array,
            },
        },
        model: {
            prop: 'modelValue',
            event: 'change',
        },
    };
</script>
<template>
    <div>
        <v-select
            :label="label"
            v-bind:value="modelValue"
            @input.native="$emit('change', $event.target.value)"
            :items="items"
        ></v-select>
    </div>
</template>

<template>
    <div class="form">
        <v-flex xs10 sm8 md6 lg5>
            <v-card>
                <FormTitle />
                <ModalFormMessage />
                <v-form ref="form" class="d-flex flex-column">
                    <FormSelect
                        v-model="vulnerabilities.vulnerability"
                        label="Vulnerability"
                        :items="items.vulnerability"
                    />
                    <FormInputs
                        v-model="vulnerabilities.evidence"
                        label="Eevidence"
                        type="file"
                    />
                    <FormInputs
                        v-model="vulnerabilities.solution"
                        label="Solution"
                        type="text"
                    />
                    <FormBtns />
                </v-form>
            </v-card>
        </v-flex>
    </div>
</template>

The select field is not returning the selected value. How can I ensure that the value I select is properly passed to the v-model in the child component as well?

Answer №1

To trigger an event named input, you should emit it using the following code:


@input="$emit('input', $event.target.value)"

Answer №2

Here is my solution:

methods: {
  updateSelectValue: function () {
    this.$emit('input', this.modelValue);
  },
},
<template>
    <div>
        <v-select
            :label="label"
            @input="updateSelectValue()"
            v-model="modelValue"
            :items="items"
        ></v-select>
        <p>{{ modelValue }}</p>
    </div>
</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

In a Next.js application directory, how can you retrieve the router.asPath value within a function that utilizes the Next.js navigation feature?

import { useRoute } from "next/navigation" const route = useRoute() const updateData = () => { route.replace(route.asPath); } I attempted using next/router but it was unsuccessful ...

What is the process for incorporating attribute values when constructing XML with fast-xml-parser?

Latest fast-xml-parser update: version 4.3.6 Description I'm trying to incorporate an xml attribute (tokenized="true") in this format : <custom-tag tokenized="true">test test &gt; 14</custom-tag> Input Code var def ...

I am looking to incorporate a password recovery page into my login process, but I am facing difficulty navigating to it once the code has been modified

I'm looking to include a forgotten password option on my login page, but I'm facing an issue when trying to navigate to it after updating the code. The website is throwing the following error message: [vue-router] uncaught error during rout ...

Is there a way to dynamically load a file on scroll using JavaScript specifically on the element <ngx-monaco-diff-editor>?

I've been attempting this task for over a week now in Angular without success. Would someone be able to provide guidance? The onContainerScroll() function isn't being triggered, and I'm considering using JavaScript instead. How can I achiev ...

Utilizing spine.js in conjunction with haml

Recently, I've been experimenting with spine.js and delving into its view documentation. In particular, the example using eco as the templating engine left me feeling less than impressed. Personally, I much prefer working with haml for my templating n ...

What is causing the permissions in firestore to not function properly?

My custom code changed(){ let reference = db.collection('messages').orderBy('timestamp') // listen for changes to the 'messages' collection reference.onSnapshot(snapshot => { snapshot.docChanges().forEach(change => ...

Passing a variable to a template in Node.js and express with hbs is causing an unexpected identifier error

I’m working on a Node.js Express app, and I’m trying to pass a variable when rendering my index.hbs file, like this: <!DOCTYPE html> <html> <body> Hello. <a href="/auth/facebook">Login with Facebook</a> {{ ...

The jQuery script tag fails to recognize click events once dynamically loaded with the load event

Hey there, I recently utilized this script in a SAP WebDynpro setup to dynamically load and employ jQuery. The onload event of the script tag is functioning well as I can select the text of the focused element. However, I am facing issues with registering ...

How can the value be accessed when using getElementById in Angular for <mat-select> elements that do not have a value attribute?

Within a loop, I have an element that has a dynamically generated id: <mat-select multiple class="dw-input" [value]="element.txn_type_id ? element.txn_type_id.split(',') : []" id="field-{{element.Name}}-txn_type_id&quo ...

Sending AJAX request within a Twitter Bootstrap modal in Symfony2

After exhausting countless Google and StackOverflow search results, I have come to the conclusion that seeking help is my best option. I am currently developing a Symfony2 application. In every view of my app, I have integrated a Twitter Bootstrap modal e ...

Reposition HTML elements using JavaScript

Is there a way to use JavaScript or jQuery to replace the image with class "fnone" located under the span element with class "ddlabel"? <span id="flat_title" class="ddTitleText "> <img class="fnone" src="images/pinc.png"> <span ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

What is the process for compiling Vue.js and Vue.min.JS?

I'm currently trying to wrap my head around the directives responsible for generating the output file(s) /dist/vue[.min].js. Upon examining the contents of the node_modules folder, I stumbled upon the /dist and /src directories. Inside the /src direct ...

What steps should I take to display a Modal upon a successful Oauth2 redirect?

I am currently working on an older web application that utilizes the portal/portlet architecture. Within the application, I have a feature that loads in a modal when accessed. The modal includes navigation functionality that allows customers to navigate t ...

Ensure that the alert for an Ajax JSON record count remains active when the count is

Trying out Ajax JSON for the first time has been a bit tricky. Even though I hard coded "Record: 1" on the server side, it keeps alerting me with a total record of 0. I'm not sure where I went wrong. Could it be an issue with how I passed the array da ...

The issue with mocha-webpack seems to be causing the DOM to not update

Using Vue with Webpack4, I installed mocha-webpack for testing. However, Mocha returned a failed result. Despite thinking that the count was updating and should pass the test, it still failed. It seems like the VirtualDom is not updating during the test ru ...

Guide to incorporating owl carousel into a Vue.js project

Hey there, I recently set up a new project using Vue CLI 3 and I'm having some trouble importing Owl Carousel 2. Here's a snippet from my main.js file: global.jQuery = require('jquery'); var $ = global.jQuery; window.$ = $; import "o ...

Activate the submission button only when all the necessary fields have been completed

I am currently working on a form that consists of 3 fields: email_from, email_subject, and an editor. The editor is built using draftjs. I am facing an issue with enabling the submit button only when all the fields are filled without any errors. Below is ...

Step-by-step guide on how to prioritize rendering the login page before the ngView in AngularJS in order to

As I begin my journey with Angular JS, I am eager to implement security measures in my application. My intention is to set up a log-in page as the default landing page for my single page application. Can anyone provide guidance or recommendations on how ...

Broadcasting TypeScript Data Structures via Socket.IO

In my Typescript code, I have created a basic User class as shown below: export class User { constructor(public id: string); } When I emit a message from my socket.io server, I include an instance of the User class like this: var user = new User(&ap ...