Cannot be bound with v-model when the vue parent component passes data to the child component through props?

  1. parent component code
    <template>
        <div>
            <item1 :test="{test:1}"></item1>
        </div>
    </template>
  1. Sub-component Code
<template>
    <div>
        <input v-model="test.test">
    </div>
</template>
<script>
    export default {
        props: {
            test: Object
        }
    }
</script>

at this time, any modification in the input box of the child component will not report an error, because the value we pass through the parent component is compound type data, and it will report an error when we change to the data of normal type, as follows

clipboard.png

the reason for reporting the error is that we modified the following

  1. parent component code
    <template>
        <div>
            <item1 :test="1"></item1>
        </div>
    </template>
  1. Sub-component Code
<template>
    <div>
        <input v-model="test">
    </div>
</template>
<script>
    export default {
        props: {
            test: Number
        }
    }
</script>

can anyone explain it?

Mar.23,2021

vue one-way data flow child component to modify the value of the parent component needs to use $emit

the reference data type is bound for the first time, and the reference address is bound. If the child component modifies the value of the reference type, vue will not report a warning.
A warning is thrown when the second underlying type value child component directly modifies the props vue passed by the parent component.


1. If the parent's ancestor passes the basic data type, in fact, the child component can be changed with v-model. What is reported to you is warning rather than an error. In other words, vue can modify the value of the parent parent directly by the child component, but is not strongly recommended .

2. Why do basic data types report warning while complex data types do not? This test is purely your understanding of the memory of the two data types in js. There are a lot of detailed information on the Internet, and you can talk a lot about it, and the subject can fill in the relevant knowledge as needed.

3. Finally, you Yuxi himself does not object to this method and can be used as a simple scheme for two-way communication between father and son components.


used to be OK. It seems that a version will be updated and a warning will be issued. Subcomponents cannot modify props data directly. There are many ways to modify it, such as triggering with emit, or building a bus line, or using vuex. Direct modification is not recommended.


you may not use the props ide/components-edge-cases.html-sharp%E8%AE%BF%E9%97%AE%E5%AD%90%E7%BB%84%E4%BB%B6%E5%AE%9E%E4%BE%8B%E6%88%96%E5%AD%90%E5%85%83%E7%B4%A0" rel=" nofollow noreferrer > link to describe

refer to this to meet your needs


the error message is not very clear. You cannot modify props directly
you can set: value.sync

Menu