Vue3封裝 Message消息提示實例函數(shù)詳解
更新時間:2021年09月24日 10:45:01 作者:run-Ameng
這篇文章主要介紹了Vue3封裝 Message消息提示實例函數(shù),具有一定的實用價值,需要的朋友可以參考下,希望能夠給你帶來幫助
Vue3封裝 消息提示實例函數(shù)
- Vue2.0使用
Vue.prototype.$message = function () {} - vue3.0使用app.config.globalProperties掛載原型方法
app.config.globalProperties.$message = Message - 也支持直接導(dǎo)入函數(shù)使用 import Message from './Message.js
樣式布局封裝 message.vue
<template>
<Transition name="down">
<div class="my-message" :style="style[type]" v-show='isShow'>
<!-- 上面綁定的是樣式 -->
<!-- 不同提示圖標會變 -->
<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: 'myMessage',
props: {
text: {
type: String,
default: ''
},
type: {
type: String,
// warn 警告 error 錯誤 success 成功
default: 'warn'
}
},
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 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;
}
}
}
.my-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>
功能實現(xiàn) message.js
//圖標
// 文本
import { createVNode,render } from 'vue'
import myMessage from './message.vue'
// 動態(tài)創(chuàng)建一個DOM容器
const div=document.createElement('div')
div.setAttribute('class','my-message-container')
document.body.appendChild(div)
export default ({text,type})=>{
let timer=null
//createVNode 用于創(chuàng)建一個虛擬節(jié)點
// 參數(shù)1 支持組件
// 參數(shù)2 表示傳遞給組件的選項
const vnode= createVNode(myMessage,{text, type})
//把虛擬的節(jié)點渲染到頁面的DOM中即可
// render函數(shù)的參數(shù)
//參數(shù)一:表示表示需要渲染的內(nèi)容(虛擬節(jié)點)
// 參數(shù)二:表示渲染的目標位置 (DOM元素)
render(vnode,div)
// 希望1s后消失
clearTimeout(timer)
timer=setTimeout(()=>{
// 清空div里面的內(nèi)容
render(null,div)
},1000)
}
// $message({ text: '登錄失敗', type: 'error'})
注冊 自定義指令
import Message from './Message.js'
export default {
install(app){
// 如果你想掛載全局的屬性,能夠通過組件實例調(diào)用的屬性 this.$message
// 擴展一個實例方法
app.config.globalProperties.$message = Message // 原型函數(shù)
}
}
使用 :
方法一
import Message from './message.js'
setup(){
Message({ text: '登錄失敗', type: 'error' })
}
方法二
// setup函數(shù)中訪問組件實例對象
import { getCurrentInstance } from 'vue'
setup(){
const instance= getCurrentInstance()
instance.proxy.$message({ text: '登錄失敗', type: 'error' })
}
方法三 this.$message
this.$message({ text: '登錄失敗', type: 'error' })
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue keep-alive列表頁緩存 詳情頁返回上一頁不刷新,定位到之前位置
這篇文章主要介紹了vue keep-alive列表頁緩存 詳情頁返回上一頁不刷新,定位到之前位置,本文通過實例代碼效果圖展示給大家介紹的非常詳細,需要的朋友可以參考下2019-11-11
使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
這篇文章主要介紹了使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過程詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
解決Vue2?axios發(fā)請求報400錯誤"Error:?Request?failed?with?s
這篇文章主要給大家介紹了關(guān)于如何解決Vue2?axios發(fā)請求報400錯誤"Error:?Request?failed?with?status?code?400"的相關(guān)資料,在Vue應(yīng)用程序中我們通常會使用axios作為網(wǎng)絡(luò)請求庫,需要的朋友可以參考下2023-07-07

