vue3組件式彈窗打開的3種方式小結(jié)
更新時間:2023年07月20日 10:17:53 作者:莫奈的日出
這篇文章主要給大家介紹了關(guān)于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="發(fā)布上線" @ok="handleOk" @cancel="handleCancel"> <template #footer> <a-button key="back" @click="handleCancel">取消</a-button> <a-button key="submit" type="primary" :loading="loading" @click="handleOk">確認發(fā)布</a-button> </template> <h1>注意事項</h1> <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 "> <li>上線時間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li> <li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時執(zhí)行慢。</li> <li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li> <li>上線頻率:少量多次,請勿積攢過多新內(nèi)容,以免影響用戶流量。</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="發(fā)布上線" @ok="openModal" @cancel="handleCancel"> <template #footer> <a-button key="back" @click="handleCancel">取消</a-button> <a-button key="submit" type="primary" :loading="loading" @click="handleOk">確認發(fā)布</a-button> </template> <h1>注意事項</h1> <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 "> <li>上線時間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li> <li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時執(zhí)行慢。</li> <li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li> <li>上線頻率:少量多次,請勿積攢過多新內(nèi)容,以免影響用戶流量。</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="發(fā)布上線" @ok="handleOk" @cancel="handleCancel"> <template #footer> <a-button key="back" @click="handleCancel">取消</a-button> <a-button key="submit" type="primary" :loading="loading" @click="handleOk">確認發(fā)布</a-button> </template> <h1>注意事項</h1> <ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 "> <li>上線時間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li> <li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時執(zhí)行慢。</li> <li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li> <li>上線頻率:少量多次,請勿積攢過多新內(nèi)容,以免影響用戶流量。</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>
總結(jié)
到此這篇關(guān)于vue3組件式彈窗打開的3種方式的文章就介紹到這了,更多相關(guān)vue3組件式彈窗打開內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE2 前端實現(xiàn) 靜態(tài)二級省市聯(lián)動選擇select的示例
下面小編就為大家分享一篇VUE2 前端實現(xiàn) 靜態(tài)二級省市聯(lián)動選擇select的示例。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue路由對不同界面進行傳參及跳轉(zhuǎn)的總結(jié)
這篇文章主要介紹了vue路由對不同界面進行傳參及跳轉(zhuǎn)的總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Vue如何獲取new Date().getTime()時間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級時間戳,而PHP后端則是秒級時間戳,處理此類問題時,需要將PHP的時間戳乘以1000轉(zhuǎn)換為毫秒級,以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10詳解vue開發(fā)中調(diào)用微信jssdk的問題
這篇文章主要介紹了vue開發(fā)中調(diào)用微信jssdk的問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04