Scenario Today, I encountered a strange problem when using [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value" This warning is caused by a custom component Vue.component("RxSelect", { model: { prop: "value", event: "change", }, props: { value: [Number, String], map: Map, }, template: ` <select v-model="value" @change="$emit('change', value)" > <option v-for="[k,v] in map" :value="k" :key="k" >{{v}}</option> </select> `, }); The code we are using seems to be fine? <main id="app"> The currently selected gender is: {{map.get(sex)}} <div> <rx-select :map="map" v-model="sex" /> </div> </main> JavaScript code new Vue({ el: "#app", data: { map: new Map().set(1, "Confidential").set(2, "Male").set(3, "Female"), sex: "", }, }); After testing, the program itself runs normally, there is no problem with the value transfer between parent and child components, and the two-way data binding does take effect, but the browser keeps reporting an error. Try to solveWe found a way
Vue.component("RxSelect", { model: { prop: "value", event: "change", }, props: { value: [Number, String], map: Map, }, data() { return { innerValue: this.value, }; }, watch: value(val) { this.innerValue = val; }, innerValue(val) { this.$emit("change", val); }, }, template: ` <select v-model="innerValue"> <option v-for="[k,v] in map" :value="k" :key="k" >{{v}}</option> </select> `, }); The usage code is exactly the same, but the code of the component solve A more elegant way is to use Vue.component("RxSelect", { model: { prop: "value", event: "change", }, props: { value: [Number, String], map: Map, }, computed: { innerValue: { get() { return this.value; }, set(val) { this.$emit("change", val); }, }, }, template: ` <select v-model="innerValue"> <option v-for="[k,v] in map" :value="k" :key="k" >{{v}}</option> </select> `, }); The above is the details of the problems and solutions encountered when Vue uses v-model to two-way bind the values of parent and child components. For more information about Vue using v-model to two-way bind the values of parent and child components, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Detailed explanation of three ways to configure Nginx virtual hosts (based on ports)
First look at the effect: Code: 1.html <div cl...
Scenario: When starting tomcat in docker (version...
The hardware requirements for deploying Hyper-V a...
<br />I have compiled some domestic design w...
Table of contents 1. Introduction 2. Use axios in...
In order to express the deep condolences of peopl...
The excellence of Linux lies in its multi-user, m...
I hope to align the title on the left and the dat...
MySQL regular sorting, custom sorting, and sortin...
Separate the front and back ends and use nginx to...
1. Overflow Overflow is overflow (container). Whe...
Table of contents 1. Check the MySQL status in th...
In the database, both UNION and UNION ALL keyword...
This article uses examples to illustrate the pitf...
Achieve results Implementation Code html <div ...