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)
This is the installation tutorial of mysql5.7.18....
1. Command Introduction The stat command is used ...
I'm very happy. When encountering this proble...
Without further ado Start recording docker pullin...
Table of contents 1. Resources and Construction 1...
In MySQL, create a view on two or more base table...
Docker Compose Docker Compose divides the managed...
Preface This article mainly introduces the releva...
Table of contents Linux 1. Basic use of crontab 2...
In addition to setting regulations for various ta...
1. First, you need to know what will trigger the v...
Table of contents Preparation Deployment process ...
Table of contents 1. Project Integration 1. CDN i...
Exposing network ports In fact, there are two par...
Founder Type Library is a font library developed ...