Between parent and child components: Click the button to pop up a dialog box to upload data. After the child component closes the dialog box, the uploaded data has a cache problem

Between parent and child components: Click the button to pop up a dialog box to upload data. After the child component closes the dialog box, the uploaded data has a cache problem

Application scenario: Check the data that needs to be operated on page A - click the button on page A - dialog B pops up - close dialog B - when returning to page A again, the checked state remains - open dialog B again - in B The list data still exists

Solution: communicate between parent and child, launch events through child components, parent component rewrites a method to close the pop-up window for the events of child components

// 子组件:
<el-dialog
    v-model="shown"
    width="720px"
    :close-on-click-modal="false"
    title="新增"
    draggable
    @close="closeDialog"
  >
   <el-button :loading="isSubmitting" @click="closeDialog">关闭</el-  button>
      
  <el-button type="primary" :loading="isSubmitting" @click="save">保存</el-dialog>

<script lang="ts">

function closeDialog() {
    
    
      emit('close');
    }
</script>
// 父组件
 <el-button size="small" type="primary" @click="openAttachment" :disabled="selection.length < 2">新增</el-button>
// 使用子组件 通过v-if控制子组件的数据是否进行展示
<Add v-if="open" :order-id="selection.map(item => item.id)" @close="closeAttachment($event)"/>


<script lang="ts">
// 引入子组件
import BatchAttachmentModal from '@/views/components/Add.vue';
export default defineComponent({
    
    
  // 注册子组件
  components: {
    
    Add},
  const open = ref<boolean>(false);
    function openAttachment() {
    
    
      open.value = true;
    }
    function closeAttachment(value) {
    
    
      open.value = false;
    }
})
</script>

Guess you like

Origin blog.csdn.net/m0_50298323/article/details/128916079