父组件:
<template>
<button @click="click">改变状态</button>
<el-aside width="200px">
<Aside :isCollapse="isCollapse" />
</el-aside>
</template>
<script setup>
const isCollapse = ref(false)
const click = () => {
isCollapse.value = !isCollapse.value
}
</sript>
子组件:
<template>
<div class="" @click="onClick">点击</div>
</template>
<script setup>
const props = defineProps({
isCollapse: {type: Boolean, required: true}
})
const computedIsCollapse = computed(() => props.isCollapse)
const isCollapse = ref(computedIsCollapse)
// 点击
const onClickIsCollapse = () => {
isCollapse.value = !isCollapse.value //提示:Write operation failed: computed value is readonly
}
</sript>
子组件从父组件获取props,并computed后,点击事件进行取反,但是不能直接对isCollapse进行操作,一般是如何处理的?
父组件:
子组件: