父组件:
<popup :show="showPopup" @closePopup="closePopup"></popup>
子组件:
<template>
<div v-if="show" @click="close">
子组件内容
</div>
</template>
<script>
export default {
name: "popup",
props:{
show: true,
},
methods:{
close(){
this.show = false;
this.$emit('Popup')
}
}
}
</script>
当我点击关闭弹窗的时候报错:
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: “show”
原因:通过props传递给子组件的show,不能在子组件内部修改props中的show值。
修改:去掉this.show = false这一行,不能在子组件内修改props的值。