vue3如何自定義message彈窗
1、定義一個Message.vue組件
其內(nèi)容如下
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]" v-show="visible">
<!-- 上面綁定的是樣式 -->
<!-- 不同提示圖標(biāo)會變 :class="{'icon-warning':true}" :class="['icon-warning']" -->
<!--這個需要用到https://www.iconfont.cn/的圖片庫-->
<!-- <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 () {
// 定義一個對象,包含三種情況的樣式,對象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)// 這個感覺可以不用,即不用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)建一個叫Message.js的文件
其內(nèi)容如下:
// 提供一個能夠顯示Message組件的函數(shù)
// 這個函數(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)
// 定時器標(biāo)識
let timer = null
export default ({ type, text }) => {
// 渲染組件
// 1. 導(dǎo)入消息提示組件
// 2. 將消息提示組件編譯為虛擬節(jié)點(diǎn)(dom節(jié)點(diǎn))
// createVNode(組件,屬性對象(props))
const vnode = createVNode(HelloWorld, { type, text })
// 3. 準(zhǔn)備一個裝載消息提示組件的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)擊左邊按鈕后會彈出一個消息提示框,3秒后關(guān)閉

5、如果想使用this.$message這種方式
則需定義一個UI.js文件用來掛載Message.js
其內(nèi)容如下:
import Message from "@/Message";
export default {
install (app) {
// 定義一個原型函數(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組件庫
// import UI from './UI'
// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(UI).mount('#app')
這種方式的缺點(diǎn)是還是得在選項(xiàng)式上才方便使用,在組合式方式上不推薦
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用@include或@mixin報(bào)錯的問題及解決
這篇文章主要介紹了vue使用@include或@mixin報(bào)錯的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue+express+Socket實(shí)現(xiàn)聊天功能
這篇文章主要為大家詳細(xì)介紹了Vue+express+Socket實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
electron-vite工具打包后如何通過內(nèi)置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項(xiàng)目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動態(tài)修改接口地址實(shí)現(xiàn)方法,需要的朋友可以參考下2024-05-05
element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時父節(jié)點(diǎn)也選中
這篇文章主要介紹了element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時父節(jié)點(diǎn)也選中的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue前端性能優(yōu)化之預(yù)加載和懶加載示例詳解
這篇文章主要為大家介紹了vue前端性能優(yōu)化之預(yù)加載和懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
vue2+elementui的el-table固定列會遮住橫向滾動條及錯位問題解決方案
這篇文章主要介紹了vue2+elementui的el-table固定列會遮住橫向滾動條及錯位問題解決方案,主要解決固定列錯位后, 接下來就是把固定列往上提滾動條的高度就不會影響了,需要的朋友可以參考下2024-01-01
vue中父子組件相互傳值的實(shí)現(xiàn)方法詳解
父子組件通信是Vue中常見的場景,這篇文章主要為大家詳細(xì)介紹了vue中父子組件相互傳值的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2023-12-12

