When doing a project, you will sometimes encounter this kind of error
this sentence means to avoid changing the prop directly, because as long as the parent component is re rendered, the value will be overridden. Instead, use data or calculate properties based on the prop value
you can’t directly change the props passed by the parent component, so what can you do?You can use emit to trigger the events of the parent component
parent component
<template>
<div class="class">
<Student :show="isShow" @hideStudent="hideStudent" />
<button @click="showStudent">点我显示学生</button>
</div>
</template>
<script>
import Student from "./student";
export default {
name: "class",
components: { Student },
data() {
return {
isShow: false,
};
},
methods: {
showStudent() {
this.isShow = true;
},
hideStudent() {
this.isShow = false;
},
},
};
</script>
Subcomponents
<template>
<div class="student" v-show="show">
<nav>Mike</nav>
<button @click="close">点我关闭student</button>
</div>
</template>
<script>
export default {
name: "student",
props: {
show: {
type: Boolean,
default:false
},
},
methods: {
close() {
this.$emit("hideStudent");
},
},
};
</script>
Of course, subcomponents can also write like this, using watch to listen
<template>
<div class="student" v-show="showStudent">
<nav>Mike</nav>
<button @click="close">点我关闭student</button>
</div>
</template>
<script>
export default {
name: "student",
props: {
show: {
type: Boolean,
default:false
},
},
data() {
return {
showStudent:false
};
},
watch:{
show(){
this.showStudent=this.show
},
},
methods: {
close() {
this.$emit("hideStudent");
},
},
};
</script>
Finally, let’s take a look at the rendering
for more details about the value transmission of vueprops, see the value transmission of Vue parent-child components
Read More:
- How to Solve VUE Error: Avoid mutating a prop directly since the value will be overwritten …
- [Solved] el-date-picker Error: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders.
- [Solved] Invalid prop: type check failed for prop “modelValue“. Expected Number…
- [Vue warn]: Invalid prop: type check failed for prop [How to Solve]
- [Vue warn]: Invalid prop: custom validator check failed for prop “index“
- [Vue warn]: Invalid prop: type check failed for prop “index“. Expected String, got Undefined
- How to Solve Props value transfer International Language error
- Full screen scrolling by Vue + Vue awesomeswiper
- Vue warn]: vue3 element Component emit Pass Event Error
- The page console error [Vue warn]: Invalid prop: custom validator check failed for prop “status“
- [Vue warn]: Invalid prop: custom validator check failed for prop “navigationBarTextStyle“.
- Vue installs sass loader directly, and node sass reports an error
- [Solved] Invalid prop: type check failed for prop “index“. Expected String, got Undefined
- Vue3.0 error: Failed to resolve component el-form-item (el element to be unable to be displayed)
- [Solved] Vue eslint Error: Component name “*****“ should always be multi-word
- [Solved] VUE D:\project\vueProject\vue-02\src\components\hello.vue 5:5 error Parsing error: x-invalid-end-tag
- Vue2.0: How to Use vue3 api to encapsulate Axios
- Please transfer a valid prop path to form item
- [Solved] Vue Error: Uncaught TypeError: Vue.createApp is not a function
- Solving routing errors by rewriting Vue push method