父組件代碼,key傳入不同的值會使組件重新渲染,不會留下之前填寫過的內容
<template> <el-form-item> <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button> </el-form-item> <!-- 子組件:彈窗, 新增 / 修改 --> <add-or-update v-if="addOrUpdateVisible" :key="addOrUpdateKey" ref="addOrUpdate" ></add-or-update> </template> <script> import AddOrUpdate from './trainingplan-add-or-update' export default { data () { return { addOrUpdateVisible: false, addOrUpdateKey: 0 } }, methods: { addOrUpdateHandle () { //key每次加1會使組件重新渲染 this.addOrUpdateKey++ this.addOrUpdateVisible = true } }, components: { AddOrUpdate, } } </script>