vue3组件式弹窗打开的3种方式小结

 更新时间:2023年07月20日 10:17:53   作者:莫奈的日出  
这篇文章主要给大家介绍了关于vue3组件式弹窗打开的3种方式,弹窗组件是常见的交互组件,它能够弹出一些提示信息、提醒用户进行操作等等,需要的朋友可以参考下

1、利用父子组件传值

父组件:

<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>
  

子组件:

<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、利用ref

父组件:

<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>
  

子组件:

<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和inject

在父组件中通过provide暴露属性或方法,子组件通过inject接受,并且只要是父组件的后代,都可以通过inject接收到父组件暴露的值

父组件:

<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>
  

子组件:

<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>

总结

到此这篇关于vue3组件式弹窗打开的3种方式的文章就介绍到这了,更多相关vue3组件式弹窗打开内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vuex的各个模块封装的实现

    Vuex的各个模块封装的实现

    这篇文章主要介绍了Vuex的各个模块封装的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 详解用vue编写弹出框组件

    详解用vue编写弹出框组件

    本篇文章主要介绍了详解用vue编写弹出框组件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 解析VUE中nextTick是什么

    解析VUE中nextTick是什么

    nextTick是Vue提供的一个全局API,由于Vue的异步更新策略导致我们对数据的修改不会立刻体现,在DOM变化上,此时如果想要立即获取更新后的DOM状态,就需要使用这个方法,这篇文章主要介绍了解析VUE中nextTick,需要的朋友可以参考下
    2022-11-11
  • Vue2中集成DHTMLXGantt甘特图的步骤

    Vue2中集成DHTMLXGantt甘特图的步骤

    文章介绍了在Vue2中集成DHTMLXGantt甘特图的步骤,涵盖安装、初始化、数据绑定、事件交互及常见问题解决,感兴趣的朋友跟随小编一起看看吧
    2025-08-08
  • vuex使用方法超详细讲解(实用)

    vuex使用方法超详细讲解(实用)

    这篇文章主要给大家介绍了关于vuex使用方法的相关资料,主要内容包括Vuex的安装和配置,以及如何在.vue文件中引入和使用Vuex状态,作者还分享了一种在模块中存储状态的建议方法,并提供了具体的代码示例,需要的朋友可以参考下
    2024-10-10
  • 深入理解Vue 的钩子函数

    深入理解Vue 的钩子函数

    这篇文章主要介绍了Vue 的钩子函数,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • uni-app自定义组件详细代码示例

    uni-app自定义组件详细代码示例

    组件是vue技术中非常重要的部分,组件使得与ui相关的轮子可以方便的制造和共享,进而使得vue使用者的开发效率大幅提升,这篇文章主要给大家介绍了关于uni-app自定义组件的相关资料,需要的朋友可以参考下
    2024-02-02
  • Vue如何动态修改el-table的某列数据

    Vue如何动态修改el-table的某列数据

    这篇文章主要介绍了Vue如何动态修改el-table的某列数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 详解Vue中$props、$attrs和$listeners的使用方法

    详解Vue中$props、$attrs和$listeners的使用方法

    本文主要介绍了Vue中$props、$attrs和$listeners的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • vue中返回结果是promise的处理方式

    vue中返回结果是promise的处理方式

    这篇文章主要介绍了vue中返回结果是promise的处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04

最新评论