vue3手動(dòng)封裝彈出框組件message的方法
本文實(shí)例為大家分享了vue3手動(dòng)封裝彈出框組件message的具體代碼,供大家參考,具體內(nèi)容如下
封裝組件
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]" v-show='isShow'>
<!-- 上面綁定的是樣式 -->
<!-- 不同提示圖標(biāo)會(huì)變 -->
<i class="iconfont" :class="[style[type].icon]"></i>
<span class="text">{{text}}</span>
</div>
</Transition>
</template>
<script>
import { onMounted, ref } from 'vue'
export default {
name: 'XtxMessage',
props: {
text: {
type: String,
default: ''
},
type: {
type: String,
// warn 警告 error 錯(cuò)誤 success 成功
default: 'warn'
}
},
setup () {
// 定義一個(gè)對(duì)象,包含三種情況的樣式,對(duì)象key就是類型字符串
const style = {
warn: {
icon: 'icon-warning',
color: '#E6A23C',
backgroundColor: 'rgb(253, 246, 236)',
borderColor: 'rgb(250, 236, 216)'
},
error: {
icon: 'icon-shanchu',
color: '#F56C6C',
backgroundColor: 'rgb(254, 240, 240)',
borderColor: 'rgb(253, 226, 226)'
},
success: {
icon: 'icon-queren2',
color: '#67C23A',
backgroundColor: 'rgb(240, 249, 235)',
borderColor: 'rgb(225, 243, 216)'
}
}
// 控制動(dòng)畫
const isShow = ref(false)
// 組件模板渲染成功后觸發(fā)
onMounted(() => {
isShow.value = true
})
return { style, isShow }
}
}
</script>
<style scoped lang="less">
.down {
&-enter {
&-from {
transform: translate3d(0, -75px, 0);
opacity: 0;
}
&-active {
transition: all 0.5s;
}
&-to {
transform: none;
opacity: 1;
}
}
}
.xtx-message {
width: 300px;
height: 50px;
position: fixed;
z-index: 9999;
left: 50%;
margin-left: -150px;
top: 25px;
line-height: 50px;
padding: 0 25px;
border: 1px solid #e4e4e4;
background: #f5f5f5;
color: #999;
border-radius: 4px;
i {
margin-right: 4px;
vertical-align: middle;
}
.text {
vertical-align: middle;
}
}
</style>
掛載到vue的原型對(duì)象上
// 如下的方法需要渲染一個(gè)提示效果
import { createVNode, render } from 'vue'
import XtxMessage from './xtx-message.vue'
// 動(dòng)態(tài)創(chuàng)建一個(gè)DOM容器
const div = document.createElement('div')
div.setAttribute('class', 'xtx-meassage-container')
document.body.appendChild(div)
export default ({ text, type }) => {
let timer = null
// createVNode 用于創(chuàng)建一個(gè)虛擬節(jié)點(diǎn)
// 參數(shù)一支持組件
// 參數(shù)二表示傳遞給組件的選項(xiàng)
const vnode = createVNode(XtxMessage, { text, type })
// 把虛擬的節(jié)點(diǎn)渲染到頁(yè)面的DOM中即可
// render函數(shù)的參數(shù)
// 參數(shù)一:表示需要渲染的內(nèi)容(虛擬節(jié)點(diǎn))
// 參數(shù)二:表示渲染的目標(biāo)位置(DOM元素)
render(vnode, div)
// 希望提示信息顯示1秒后消失
clearTimeout(timer)
timer = setTimeout(() => {
// 清空div中的內(nèi)容
render(null, div)
}, 1000)
}
// $message({ text: '登錄失敗', type: 'error'})
import Message from './Message'
export default {
install(app) {
// 如果你想掛載全局的屬性,能夠通過(guò)組件實(shí)例調(diào)用的屬性 this.$message
app.config.globalProperties.$message = Message // 原型函數(shù)
}
}
使用
一 .
import Message from '@/components/library/Message'
setup () {
// 觸發(fā)登錄
const handleLogin = async () => {
// 手動(dòng)進(jìn)行表單驗(yàn)證
const flag = await target.value.validate()
if (flag) {
// 驗(yàn)證通過(guò),調(diào)用接口實(shí)現(xiàn)登錄
// 如果登錄失敗,就進(jìn)行提示
Message({ type: 'error', text: '登錄失敗' })
}
}
mounted () {
this.$message({ type: 'error', text: '登錄失敗' })
}
}
二.
// 獲取組件的實(shí)例對(duì)象:類似于之前的this
const instance = getCurrentInstance()
// 點(diǎn)擊登錄
const handleLogin = async () => {
const valid = await target.value.validate()
if (valid) {
// 表單驗(yàn)證通過(guò),調(diào)用接口實(shí)現(xiàn)登錄
// Message({ text: '登錄失敗', type: 'error' })
instance.proxy.$message({ text: '登錄失敗', type: 'error' })
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element動(dòng)態(tài)路由面包屑的實(shí)現(xiàn)示例
本文主要介紹了element動(dòng)態(tài)路由面包屑的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
web面試MVC與MVVM區(qū)別及Vue為什么不完全遵守MVVM解答
這篇文章主要介紹了web面試中常問(wèn)問(wèn)題,MVC與MVVM區(qū)別以及Vue為什么不完全遵守MVVM的難點(diǎn)解答,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
Vue安裝sass-loader和node-sass版本匹配的報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了Vue安裝sass-loader和node-sass版本匹配的報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
解決vue動(dòng)態(tài)路由異步加載import組件,加載不到module的問(wèn)題
這篇文章主要介紹了解決vue動(dòng)態(tài)路由異步加載import組件,加載不到module的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue的transition-group與Virtual Dom Diff算法的使用
這篇文章主要介紹了Vue的transition-group與Virtual Dom Diff算法的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue-resource攔截器設(shè)置頭信息的實(shí)例
下面小編就為大家?guī)?lái)一篇vue-resource攔截器設(shè)置頭信息的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
解決vue無(wú)法設(shè)置滾動(dòng)位置的問(wèn)題
這篇文章主要介紹了解決vue無(wú)法設(shè)置滾動(dòng)位置的問(wèn)題 ,需要的朋友可以參考下2018-10-10
vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明
這篇文章主要介紹了vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

