问题描述
点击父组件中左侧表格行时,会传新值到右侧子组件富文本框,但是如果新值为null时,富文本框的旧值不会被清空
解决方案
利用computed计算属性里的get方法,判断props传过来的值为空时,
返回空标签'<p></p>'
,这样当父组件传过来的值为空时,组件值也会更新
示例关键代码:
<template>
<div >
<Editor v-model="valueHtml" />
</div>
</template>
<script setup lang="ts">
const props = defineProps({
content: {
type: String
}
})
const valueHtml = computed({
get() {
if (props.content) {
return props.content
} else {
// 为空值的时候返回这个,要不然富文本组件不会把null值直接更新到页面显示
return '<p></p>'
}
},
set(newvalue) {
emit('update:content', newvalue)
}
})
</script>