vue3如何自定義message彈窗
1、定義一個(gè)Message.vue組件
其內(nèi)容如下
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]" v-show="visible">
<!-- 上面綁定的是樣式 -->
<!-- 不同提示圖標(biāo)會(huì)變 :class="{'icon-warning':true}" :class="['icon-warning']" -->
<!--這個(gè)需要用到https://www.iconfont.cn/的圖片庫(kù)-->
<!-- <i class="iconfont" :class="[style[type].icon]"></i>-->
<span class="text">{{text}}</span>
</div>
</Transition>
</template>
<script>
import { ref } from 'vue'
export default {
name: 'Message',
props: {
type: {
type: String,
default: 'warn'
},
text: {
type: String,
default: ''
}
},
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)'
}
}
// 控制元素顯示隱藏
// const visible = ref(false)
const visible = ref(true)// 這個(gè)感覺可以不用,即不用v-show
// onMounted(() => {
// visible.value = true
// })
return { style, visible }
}
}
</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>2、創(chuàng)建一個(gè)叫Message.js的文件
其內(nèi)容如下:
// 提供一個(gè)能夠顯示Message組件的函數(shù)
// 這個(gè)函數(shù)將來:導(dǎo)入直接使用,也可以掛載在vue實(shí)例原型上
import { createVNode, render } from 'vue'
import HelloWorld from "@/components/Message.vue";
// DOM容器
const div = document.createElement('div')
div.setAttribute('class', 'xtx-msssage-container')
document.body.appendChild(div)
// 定時(shí)器標(biāo)識(shí)
let timer = null
export default ({ type, text }) => {
// 渲染組件
// 1. 導(dǎo)入消息提示組件
// 2. 將消息提示組件編譯為虛擬節(jié)點(diǎn)(dom節(jié)點(diǎn))
// createVNode(組件,屬性對(duì)象(props))
const vnode = createVNode(HelloWorld, { type, text })
// 3. 準(zhǔn)備一個(gè)裝載消息提示組件的DOM容器
// 4. 將虛擬節(jié)點(diǎn)渲染再容器中
// render(虛擬節(jié)點(diǎn),DOM容器)
render(vnode, div)
// 5. 3s后銷毀組件
clearTimeout(timer)
timer = setTimeout(() => {
render(null, div)
}, 3000)
}
3、在app.vue中導(dǎo)入并使用
<template>
<div class="btn" @click="btn">點(diǎn)擊彈出消息提示框</div>
</template>
<script setup>
import Message from "@/Message";
import {onMounted} from "vue";
const btn = ()=>{
Message({type:'warn', text:"hello world"})
}
// 這種方式雖然也可以但是不推薦
// onMounted(()=>{
// this.$message({type:'warn', text:"hello world"})
// })
</script><style lang="less" scoped>
.btn{
width: 300px;
height: 40px;
text-align: center;
line-height: 40px;
background-color: deeppink;
}
</style>4、效果如下
當(dāng)點(diǎn)擊左邊按鈕后會(huì)彈出一個(gè)消息提示框,3秒后關(guān)閉

5、如果想使用this.$message這種方式
則需定義一個(gè)UI.js文件用來掛載Message.js
其內(nèi)容如下:
import Message from "@/Message";
export default {
install (app) {
// 定義一個(gè)原型函數(shù)
app.config.globalProperties.$message = Message
}
}6、在main.js中導(dǎo)入并使用use掛載到app上
如下所示:
import { createApp } from 'vue'
import App from './App.vue'
import UI from "@/UI";
// 導(dǎo)入自己UI組件庫(kù)
// import UI from './UI'
// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(UI).mount('#app')
這種方式的缺點(diǎn)是還是得在選項(xiàng)式上才方便使用,在組合式方式上不推薦
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Proxy如何實(shí)現(xiàn)Vue響應(yīng)式
這篇文章主要是來和大家探討一下,Vue的響應(yīng)式系統(tǒng)僅僅是一個(gè)Proxy嗎,本文將圍繞此問題探索一下Proxy是如何實(shí)現(xiàn)Vue響應(yīng)式的,感興趣的小伙伴可以了解一下2023-08-08
vue如何封裝自己的Svg圖標(biāo)組件庫(kù)(svg-sprite-loader)
這篇文章主要介紹了vue如何封裝自己的Svg圖標(biāo)組件庫(kù)(svg-sprite-loader),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue3中安裝并使用CSS預(yù)處理器Sass的方法詳解
Sass是一種CSS預(yù)處理器,它擴(kuò)展了CSS的功能,提供了更高級(jí)的語(yǔ)法和特性,例如變量、嵌套、混合、繼承和顏色功能等,這些特性可以幫助開發(fā)者更高效地管理和維護(hù)樣式表,本文介紹vue3中安裝并使用CSS預(yù)處理器Sass的方法,感興趣的朋友一起看看吧2024-01-01
Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法
今天小編就為大家分享一篇Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
FastAPI和Vue實(shí)現(xiàn)文件分片上傳+秒傳+斷點(diǎn)續(xù)傳的完整思路
本文介紹了使用FastAPI和Vue實(shí)現(xiàn)大文件分片上傳的方法,包括文件切片、哈希校驗(yàn)、秒傳和斷點(diǎn)續(xù)傳等核心概念,并提供了后端FastAPI和前端Vue的實(shí)現(xiàn)思路及關(guān)鍵代碼片段,感興趣的朋友跟隨小編一起看看吧2026-04-04

