vue3組件式彈窗打開的3種方式小結(jié)
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">確認(rèn)發(fā)布</a-button>
</template>
<h1>注意事項(xiàng)</h1>
<ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
<li>上線時(shí)間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li>
<li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時(shí)執(zhí)行慢。</li>
<li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li>
<li>上線頻率:少量多次,請(qǐng)勿積攢過多新內(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: '錯(cuò)誤提示',
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">確認(rèn)發(fā)布</a-button>
</template>
<h1>注意事項(xiàng)</h1>
<ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
<li>上線時(shí)間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li>
<li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時(shí)執(zhí)行慢。</li>
<li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li>
<li>上線頻率:少量多次,請(qǐng)勿積攢過多新內(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: '錯(cuò)誤提示',
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">確認(rèn)發(fā)布</a-button>
</template>
<h1>注意事項(xiàng)</h1>
<ol style=" listStyleType: 'decimal', paddingLeft: 12, marginTop: 10 ">
<li>上線時(shí)間:19:00-23:00禁止(流量高峰),緊急上線聯(lián)系開發(fā)。</li>
<li>上線間隔:3分鐘內(nèi)只能上線一次,防止新內(nèi)容過多時(shí)執(zhí)行慢。</li>
<li>上線成功:出現(xiàn)“上線成功”彈窗,即完成本次上線。</li>
<li>上線頻率:少量多次,請(qǐng)勿積攢過多新內(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: '錯(cuò)誤提示',
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?css?相對(duì)路徑導(dǎo)入問題級(jí)踩坑記錄
這篇文章主要介紹了vue?css?相對(duì)路徑導(dǎo)入問題級(jí)踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間
這篇文章主要介紹了vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例
下面小編就為大家分享一篇VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié)
這篇文章主要介紹了vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue如何獲取new Date().getTime()時(shí)間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級(jí)時(shí)間戳,而PHP后端則是秒級(jí)時(shí)間戳,處理此類問題時(shí),需要將PHP的時(shí)間戳乘以1000轉(zhuǎn)換為毫秒級(jí),以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10
vue實(shí)現(xiàn)手機(jī)驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)手機(jī)驗(yàn)證碼登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
詳解vue開發(fā)中調(diào)用微信jssdk的問題
這篇文章主要介紹了vue開發(fā)中調(diào)用微信jssdk的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

