Method 1: The child component sends emit, triggers the parent component to modify
Parent component
<template> <div> <son :count="count" @updateCount="updateCount" /> </div> </template> <script> import son from "./son"; export default { data() { return { count: 0, }; }, components: { son }, methods: { updateCount(data) { = data; }, }, }; </script>
Subcomponents
<template> <div class="goodsBasic"> <div>Parent component:{{ count }}</div> <el-button @click="changeCount">Way1</el-button> </div> </template> <script> export default { props: { count: { type: Number, default: 0, }, }, methods: { changeCount() { this.$emit("updateCount", + 1); }, }, }; </script>
Method 2: Force modification in child components
Parent component
<template> <div> <son :="text" /> </div> </template> <script> import son from "./son"; export default { data() { return { text: "hello world", }; }, components: { son }, }; </script>
Subcomponents
<template> <div class="goodsBasic"> <div>Parent component:{{ text }}</div> <el-button @click="changeCount">Way2</el-button> </div> </template> <script> export default { props: { text: { type: String, default: "", }, }, methods: { changeCount() { this.$emit("update:text", "I've been modified forcibly"); }, }, }; </script>
Method 3: The child component defines a value to replace the value passed by the parent component (not recommended, the value of the parent and child component of this method is not modified synchronously)
Parent component
<template> <div> <son :count="count" /> </div> </template> <script> import son from "./son"; export default { data() { return { count: 0, }; }, components: { son }, }; </script>
Subcomponents
<template> <div class="goodsBasic"> <div>Subcomponents:{{ son_count }}</div> <el-button @click="changeCount">Way3</el-button> </div> </template> <script> export default { props: { count: { type: Number, default: 0, }, }, data() { return { son_count: , }; }, methods: { changeCount() { this.son_count++; }, }, }; </script>
The above is a detailed explanation of the three ways in which child components in Vue modify the values transmitted by the parent component. For more information about Vue subcomponents modifying the values transmitted by the parent component, please pay attention to my other related articles!