The v-model of one component causing interference with the v-model of another component

I am currently utilizing vue js alongside buefy and have implemented two components: a field-radiobox component and a field-textarea component.

Within my post.cshtml page, I can choose an option from the radio buttons, and the selected value is appropriately displayed in the input box below within the respective component.

However, when I make edits inside the textbox of the field-textbox component, the previous selection from the field-radiobox component is completely erased. The input box below the radio buttons also no longer shows anything, indicating that the v-model in that particular component was reset or disrupted while typing in the textbox.

Interestingly, this issue only seems to occur with a textbox element. If I were to employ an input element instead of a textbox, the problem does not manifest.

Why is it that the v-model of one component is interfering with the v-model of another component?

post.cshtml

<div>
<form data-toggle="formcache" class="form rounded justify-content-center" id="form" asp-route-returnUrl="@ViewData["ReturnUrl"]" method="post" v-cloak>
    <div asp-validation-summary="All" class="text-danger"></div>

    <!--Select Advert type-->
    <field-radiobox 
        asp-property="TypeId"
        title="I want to" 
        :options="advertTypes" 
        value-key="id" 
        label-key="name"></field-radiobox>

    <!--TERMS-->
    <field-textarea asp-property="Terms" title="Terms"></field-textarea>
</form>
</div>
<script>
        var vm = new Vue({
            el: '#form',
            data:
            {
                loaded: false,
                advertTypes: @Html.Raw(Json.Serialize(Model.AdvertTypes))
            }
        });
</script>

field-radiobox.vue

<template>
    <!-- https://buefy.github.io/#/documentation/dropdown -->
    <div>
    <b-field :label="title" horizontal>
        <b-radio-button v-model="selectedValue"
                        v-for="option in options"
                        :native-value="getValue(option)" expanded>
            {{ getLabel(option) }}
        </b-radio-button>
    </b-field>
        <input :asp-for="aspProperty" v-model="selectedValue"/>
    </div>
</template>

<script>
    export default {
        name: "field-radiobox",
        data: function() {
            return {
                selectedValue: this.selectedValue
            }
        },
        props: {
            title: {
                type: String,
                required: true
            },
            options:{
                type:Array,
                required:true
            },
            valueKey:{
                type:String,
                required:true
            },
            labelKey:{
                type:String,
                required:true
            },
            selectedValue: {
                required:false
            },
            aspProperty:{
                type:String,
                required:false
            }
        },
        methods: {
            getLabel(obj) {
                //return this.labelKey;
                return !this.labelKey ? obj : obj[this.labelKey]
            },
            getValue(obj) {
                return !this.valueKey ? obj : obj[this.valueKey]
            }
        }
    }
</script>

field-text.vue

<template>
    <!-- https://buefy.github.io/#/documentation/dropdown -->
    <div>
        <b-field :label="title" horizontal>
            <textarea :class="inputClass" :readonly="readOnly"
                      :placeholder="placeholder" v-model="inputValue"></textarea>
        </b-field>
            <input :asp-for="aspProperty" v-model="inputValue"/>
    </div>
</template>

<script>
    export default {
        name: "field-textarea",
        data: function () {
            return {
                inputValue: this.inputValue
            }
        },
        props: {
            title: {
                type: String,
                required: true
            },
            inputClass: {
                type: String,
                required: false,
                default: "textarea"
            },
            placeholder: {
                type: String,
                required: false
            },
            readOnly: {
                type: Boolean,
                required: false,
                default: false
            },
            inputValue: {
                type: String,
                required: false,
                default: ""
            },
            aspProperty:{
                type:String,
                required:false
            }
        },
        methods: {
            getLabel(obj) {
                //return this.labelKey;
                return !this.labelKey ? obj : obj[this.labelKey]
            },
            getValue(obj) {
                return !this.valueKey ? obj : obj[this.valueKey]
            }
        }
    }
</script>

<style scoped>

</style>

Answer №1

It is important to avoid naming your data items the same as your prop items, especially when they are promoted to top-of-instance items. This can lead to confusion and potential issues with visibility of the prop. It's recommended to bind $data.inputValue instead or choose different names for clarity.

(You may be receiving warnings about modifying props if this issue persists.)

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

What is the best way to display index.ejs when the input field is blank?

If the input field is left empty when I click the form button, I want to redirect to "/". After clicking the button, the desired URL should be: http://localhost:3000 Header.ejs <form action="/search" method="GET"> < ...

Inexperienced JavaScript user looking for help with handling XMLHttpRequest response data

It's been well over a decade since I last dabbled in JavaScript, so here I am again. My goal is to create a dynamic graph that updates in real time using data from either my backend database or my ESP32 micro-controller. While it's easy to genera ...

Can global Vue 3 plugins be utilized within Storybook narratives?

Is it possible to integrate a Vue 3 plugin into Storybook stories? One example is the use of FormKit for creating form elements within components. I have globally set up FormKit in my main.ts file like this: import { plugin, defaultConfig } from '@f ...

Change the background color of a MUI ToggleButton based on a dynamic selection

const StyledToggleButton = styled(MuiToggleButton)(({ selectedColor }) => ({ "&.Mui-selected, &.Mui-selected:hover": { backgroundColor: selectedColor, } })); const FilterTeam = (props) => { const [view, setView] = ...

Tips for effectively managing asynchronous tasks

I keep getting different numbers every time my code runs. Can you tell me if I'm doing this the right way? Here's the code: export class GetPlanetsService { url='https://swapi.co/api/planets/?page='; planets:Planet[]=[]; headers: ...

Errors in Chartist.js Data Types

I am currently using the Chartist library to monitor various metrics for a website, but I have encountered some challenges with the plotting process. The main errors that are appearing include: TypeError: a.series.map is not a function TypeError: d.normal ...

Could the characters in an input field be colored alternately?

Have you ever wondered if it's possible to alternate the color of each character in an input field? For example, switching between red, green, and blue for each character. I've seen people achieve this by dynamically wrapping characters in a span ...

Transfer a session ID cookie generated by express-session to a React front end on a different domain

I am currently developing a web application. The back end of my app is built using Node.js and Express.js, with a specific focus on utilizing the express-session module for implementing session-based authentication to maintain user logins. I understand tha ...

Encountering the error message "{error: 'Operation `users.findOne()` buffering timed out after 10000ms'}" while trying to access my app on localhost

In my project, I have organized my code into 'client' and 'server' folders. The server side of the application is built using Express and successfully connected to a MongoDB database in the server folder using nodemon. https://i.sstati ...

WebDriverIO effortlessly converts the text extracted using the getText() command

One of my webpage elements contains the following text: <span class="mat-button-wrapper">Sicherheitsfrage ändern</span> However, when I attempt to verify this text using webdriver, it indicates that it is incorrect assert.strictEqual($(.mat ...

How can I enhance this conversion function from an Array to an Object in JavaScript?

Looking to construct an object consisting of empty arrays using values as keys. const CATEGORIES = ['apple', 'banana', 'orange'] generateCategoryObject() === { apple: [], banana: [], orange: []} function generateCategoryO ...

What could be the reason for the issue `injection "store" not found` showing up when attempting to call `this.store.commit()`?

I am currently working on storing values with VueX, specifically using Vuex 4.0 instead of the older version 3.0. While attempting to commit a value, I encountered an issue as follows: ... this.$store.commit("changerusername", u) ... To addres ...

Experience the ability to dynamically alter an image in a 360-degree

<div style="margin-bottom: 200px"> <a href="#" onclick="cambio()"> sdsadasdas</a> </div> <div id="container"></div> <div id="info"> </div> When I click on a button, I want to change the image dynamica ...

How can I display every index from my JSON Fetched Files?

In the picture shown here, I have a series of Tables being displayed: https://i.sstatic.net/YUZD1.png The issue highlighted in red is that I want to show the Index of each JSON array as the Table number. Below is the code snippet: function getExternal( ...

Troubleshooting issue with rendering <li></> using array.map: Map's return statement is producing undefined output

Trying to implement pagination in a React project and facing issues with rendering a set of li elements within an ul. The renderPageNumbers function is returning undefined in the console, even though I can see the array being passed and logging out each el ...

Is there a way to preserve the context in React when transitioning to a new page?

Currently, I am encountering a challenge within my React application involving multiple components, each utilizing its own context through the useContext hook. My goal is to uphold the context for each component even when there are changes in the URL. I a ...

Running a JavaScript test using the Mocha framework with an 'import' statement for a JS file

I am familiar with the module.export and require method: Requiring external js file for mocha testing While it is useful when dealing with modules, I find it inconvenient as I now need to test code in a specific file. For example, I have code in a file: ...

AngularJS allows you to dynamically disable a button based on a value in an array

I have an array containing letters from A to Z and I want to create a list of buttons using them. $scope.alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); I also have another array: $scope.uniqChar = ['a', ' ...

Puppeteer: Locating elements using HTML attributes

I'm currently working on getting Puppeteer to locate an element on this webpage using its attribute data-form-field-value, which needs to match 244103310504090. Here is the HTML code for the button in question: <section class="fl-accordion-tab--c ...

I'm attempting to install the "firebase" package using npm, but I keep encountering a python-related error message during the installation process

I am experiencing difficulties while attempting to install the firebase package in a local expo-managed project. Unfortunately, I keep receiving the following error message... Here is the error message that I am encountering I have already tried using "e ...