Three ways to open vue3-component pop-up window

Table of contents

1. Use parent-child components to pass values

2. Use ref

3. provide and inject


1. Use parent-child components to pass values

parent component:

<template>
  <div>
    <a-button style="margin: 20px" type="primary" @click="showModal">上线</a-button>
    <OnlineModal :controlVisible="visibleIt" @closeModal="visibleIt=false"/>
  </div>
</template>
  
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRoute } from 'vue-router'
import OnlineModal from './onlineModal.vue'
export default defineComponent({
  components: {
    OnlineModal
  },
  setup() {
    const route = useRoute()
    const visibleIt = ref<boolean>(false)
    const showModal = () => {
      visibleIt.value = true
    }
    return {
      route,
      visibleIt,
      showModal
    }
  }
})
</script>
  

Subassembly:

<template>
  <a-modal :visible="controlVisible" title="发布上线" @ok="handleOk" @cancel="handleCancel">
    <template #footer>
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk">确认发布</a-button>
    </template>
    <h1>注意事项</h1>
          <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
            <li>上线时间:19:00-23:00禁止(流量高峰),紧急上线联系开发。</li>
            <li>上线间隔:3分钟内只能上线一次,防止新内容过多时执行慢。</li>
            <li>上线成功:出现“上线成功”弹窗,即完成本次上线。</li>
            <li>上线频率:少量多次,请勿积攒过多新内容,以免影响用户流量。</li>
          </ol>
  </a-modal>
</template>
<script lang="ts">
import {  ref } from 'vue'
import {postRelease} from '@/services/online'
import { message } from 'ant-design-vue';
export default ({
  props:['controlVisible'],
  setup(props, {emit}) {
    console.log(props.controlVisible);
    const loading = ref<boolean>(false)
    const handleOk = () => {
      loading.value = true
      postRelease().then( res => {
        console.log(res, '----');
        debugger
        message.success('上线成功')
        loading.value = false
     }).catch(err => {
        message.error({
            title: '错误提示',
            content: err?.response?.data?.msg || '上线失败'
        })
        loading.value = false
     })
      
      emit('closeModal')
    }
    const handleCancel = () => {
        emit('closeModal')
    }
    return {
       loading,
        handleOk,
      handleCancel
    }
  }
})
</script>

2. Use ref

parent component:

<template>
  <div>
    <a-button style="margin: 20px" type="primary" @click="showModal">上线</a-button>
    <OnlineModal ref="onlineModal" />
  </div>
</template>
  
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRoute } from 'vue-router'
import OnlineModal from './onlineModal.vue'
export default defineComponent({
  components: {
    OnlineModal
  },
  setup() {
    const route = useRoute()
    const onlineModal = ref<boolean>(false)
    const showModal = () => {
      onlineModal.value.openModal()
    }
    return {
      route,
      showModal,
      onlineModal
    }
  }
})
</script>
  

Subassembly:

<template>
  <a-modal :visible="controlVisible" title="发布上线" @ok="openModal" @cancel="handleCancel">
    <template #footer>
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk">确认发布</a-button>
    </template>
    <h1>注意事项</h1>
          <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
            <li>上线时间:19:00-23:00禁止(流量高峰),紧急上线联系开发。</li>
            <li>上线间隔:3分钟内只能上线一次,防止新内容过多时执行慢。</li>
            <li>上线成功:出现“上线成功”弹窗,即完成本次上线。</li>
            <li>上线频率:少量多次,请勿积攒过多新内容,以免影响用户流量。</li>
          </ol>
  </a-modal>
</template>
<script lang="ts">
import {  ref } from 'vue'
import {postRelease} from '@/services/online'
import { message } from 'ant-design-vue';
export default ({
  setup(props, {emit}) {
    const controlVisible = ref<boolean>(false)
    const loading = ref<boolean>(false)
    const openModal = () =>{
        controlVisible.value = true
    }
    const handleOk = () => {
        openModal()
      loading.value = true
      postRelease().then( res => {
        console.log(res, '----');
        debugger
        message.success('上线成功')
        loading.value = false
        handleCancel()
     }).catch(err => {
        message.error({
            title: '错误提示',
            content: err?.response?.data?.msg || '上线失败'
        })
        loading.value = false
        handleCancel()
     })
    }
    const handleCancel = () => {
        controlVisible.value = false
    }
    return {
       loading,
        handleOk,
        openModal,
      handleCancel,
      controlVisible
    }
  }
})
</script>

3. provide and inject

In the parent component, expose the property or method through provide, and the child component accepts it through inject, and as long as it is a descendant of the parent component, it can receive the value exposed by the parent component through inject

parent component:

<template>
  <div>
    <a-button style="margin: 20px" type="primary" @click="showModal">上线</a-button>
    <OnlineModal />
  </div>
</template>
  
<script lang="ts">
import { defineComponent, provide, ref } from 'vue'
import { useRoute } from 'vue-router'
import OnlineModal from './onlineModal.vue'
export default defineComponent({
  components: {
    OnlineModal
  },
  setup() {
    const route = useRoute()
    const controlIfVisible = ref<boolean>(false)
    provide('controlIfVisible', controlIfVisible)
    const showModal = (sonValue) => {
      controlIfVisible.value = sonValue
    }
    provide('handle', showModal)
    return {
      route,
      showModal,
      controlIfVisible
    }
  }
})
</script>
  

Subassembly:

<template>
  <a-modal :visible="controlVisible" title="发布上线" @ok="handleOk" @cancel="handleCancel">
    <template #footer>
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk">确认发布</a-button>
    </template>
    <h1>注意事项</h1>
    <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
      <li>上线时间:19:00-23:00禁止(流量高峰),紧急上线联系开发。</li>
      <li>上线间隔:3分钟内只能上线一次,防止新内容过多时执行慢。</li>
      <li>上线成功:出现“上线成功”弹窗,即完成本次上线。</li>
      <li>上线频率:少量多次,请勿积攒过多新内容,以免影响用户流量。</li>
    </ol>
  </a-modal>
</template>
<script lang="ts">
import { ref, inject } from 'vue'
import { postRelease } from '@/services/online'
import { message } from 'ant-design-vue'
export default {
  setup(props) {
    const loading = ref<boolean>(false)
    const controlVisible = inject('controlIfVisible')
    const handle: any = inject('handle')
    const handleOk = () => {
      handle(true)
      loading.value = true
      postRelease()
        .then((res) => {
          console.log(res, '----')
          debugger
          message.success('上线成功')
          loading.value = false
          handleCancel()
        })
        .catch((err) => {
          message.error({
            title: '错误提示',
            content: err?.response?.data?.msg || '上线失败'
          })
          loading.value = false
          handleCancel()
        })
    }
    const handleCancel = () => {
      handle(false)
    }
    return {
      loading,
      handleOk,
      handleCancel,
      controlVisible
    }
  }
}
</script>

Guess you like

Origin blog.csdn.net/weixin_52993364/article/details/130045309