? Here comes Chestnut, please check
parent component
<template>
<div>
<child :data="data" :obj="obj"/>
<h1>data: {{data}}</h1>
<h1>objData: {{obj.data}}</h1>
</div>
</template>
<script>
import child from './child'
export default {
data(){
return {
data: '',
obj: {
data: ''
}
}
},
components: { child }
}
</script>
subcomponents
<template>
<div>
<button @click="change">click to change prop data</button>
<button @click="changeObjData">click to chnage prop objData</button>
</div>
</template>
<script>
export default {
props: ['data', 'obj'],
methods: {
change(){
this.data = Math.random()
},
changeObjData(){
this.obj.data = Math.random()
}
}
}
</script>
two buttons, one is prop that changes the value type, and the other is prop that changes the reference type of . You will find that the first button will report an error and the value will not be changed, but when you press the second button, the value will change and no error will be reported. That is because what your parent component passes is only a reference. To put it bluntly, it passes a memory address (pointer), and the child component changes the above value. The pointer is not changed, so the system assumes that it does not cause a change in the value of the parent component, so there is no error prompt.
move on
prop :
Prop ;
Prop
:
prop :
props: ['initialCounter'],
data: function () {
return { counter: this.initialCounter }
}
prop :
props: ['size'],
computed: {
normalizedSize: function () {
return this.size.trim().toLowerCase()
}
}
if the value passed in by props is a reference type, change its element in the child component without changing the reference, then no error is reported.
if it is an ordinary type, then the browser console will report an error message when modifying
one-way data browsing does not allow you to modify. If you can modify it, each component can operate on the data, so it cannot effectively track the changes of the data
.