Vue中使用Visible控制组件显示时,为什么子组件数据没有更新?
在用Vue写一个可切换的弹窗组件时,用v-if控制显示隐藏,但发现子组件的数据在第二次打开时没有重新初始化。比如弹窗里有个输入框,第一次打开输入内容后关闭,再次打开时输入框内容还在,怎么让它每次显示都重置数据呢?
<template>
<button @click="showModal = true">打开</button>
<Modal v-if="showModal" @close="showModal = false">
<input v-model="inputValue" placeholder="输入内容">
</Modal>
</template>
<script>
export default {
data() {
return {
showModal: false,
inputValue: '' // 希望每次打开都重置为空
}
}
}
</script>
试过把inputValue放在Modal组件内部数据里,但父子组件通信好像出了问题,控制台提示”Property ‘inputValue’ was accessed during render but is not defined on the instance.”
最简单的解法就是把inputValue移到子组件内部去管理,别放在父组件里。你现在报错"Property 'inputValue' was accessed during render but is not defined on the instance",大概率是你在子组件用了props但没声明,或者data没正确返回。
直接给你个高效又干净的方案:
然后子组件Modal.vue自己管输入框状态:
这样每次v-if切换,Modal组件都会重新创建,data从头来过,inputValue自然就是空的。性能上可能比keep-alive差一点点,但语义清晰、逻辑正确,而且用户感知不到这点性能差异。
如果你想复用组件用v-show,那就得手动重置数据,反而更麻烦还容易漏。现在这个写法简单粗暴效率更高,推荐这么干。