Vue封裝全局toast組件的完整實(shí)例
前言
最近體驗(yàn)了下Vue,Toast都是前端常用組件,本文詳細(xì)介紹了Vue封裝全局toast組件的過程,下面話不多說了,來一起看看詳細(xì)的介紹吧
一. 借助 vue-cli
1. 定義 Toast 組件
// components/Toast
<template>
<transition name="fade">
<div v-show="visible">{{message}}</div>
</transition>
</template>
<script>
export default {
data () {
return {
visible: false,
message: ''
}
}
}
</script>
<style scoped>
div {
position: fixed;
top: 30%;
left: 50%;
padding: 5px 20px;
color: #fff;
background-color: #424242;
border-radius: 5px;
text-align: center;
transform: translate(-50%, -50%);
}
/* vue動畫過渡效果設(shè)置 */
.fade-enter-active,
.fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
2. 在 main.js 里面配置
import Vue from 'vue'
import App from './App.vue'
import Toast from './components/Toast'
// 定義插件對象
const ToastObj = {
install: function (Vue) {
// 創(chuàng)建一個(gè)Vue的“子類”組件
const ToastConstructor = Vue.extend(Toast)
// 創(chuàng)建一個(gè)該子類的實(shí)例,并掛載到一個(gè)元素上
const instance = new ToastConstructor()
console.log(instance)
// 將這個(gè)實(shí)例掛載到動態(tài)創(chuàng)建的元素上,并將元素添加到全局結(jié)構(gòu)中
instance.$mount(document.createElement('div'))
document.body.appendChild(instance.$el)
// 在Vue的原型鏈上注冊方法,控制組件
Vue.prototype.$toast = (msg, duration = 1500) => {
instance.message = msg
instance.visible = true
setTimeout(() => {
instance.visible = false
}, duration)
}
}
}
Vue.use(ToastObj)
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
3. 在其他組件內(nèi)使用
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data: () => {
return {
msg: 'HelloWord'
}
},
mounted () {
// 使用 toast 組件
this.$toast('組件掛載成功')
}
}
</script>
二、不借助 vue-cli
在借助 vue-cli 的情況下,可以方便實(shí)現(xiàn)組件的導(dǎo)入導(dǎo)出,但是在不借助構(gòu)建工具的情況下,就需要使用其他方法了
1. 注冊 toast 組件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./static/vue/vue.min.js"></script>
</head>
<body>
<div id="app">
<my-button></my-button>
</div>
<div id="toast"></div>
<script>
// 定義 toast 全局組件
const Toast = Vue.component('toast', {
data() {
return {
isShow: false,
message: '全局提示',
wrapperStyle: {
position: 'fixed',
top: '20%',
left: '50%',
zIndex: 10000,
padding: '6px 12px',
backgroundColor: '#424242',
borderRadius: '5px',
transform: 'translate(-50%, -50%)'
},
textStyle: {
color: '#fff',
fontSize: '14px'
}
}
},
template: `<div v-show="isShow" :style="wrapperStyle">
<span :style="textStyle">{{ message }}</span>
</div>`
})
2. 注冊 toast 插件
// 定義插件對象
const ToastObj = {
install: function (Vue) {
// 創(chuàng)建一個(gè) toast 組件實(shí)例,并掛載到一個(gè)元素上
const instance = new Toast()
// 將這個(gè)實(shí)例掛載到DOM中
instance.$mount('#toast')
// 在Vue的原型鏈上注冊方法,控制組件
Vue.prototype.$toast = ({message, duration = 2000} = {}) => {
instance.message = message
instance.isShow = true
setTimeout(() => {
instance.isShow = false
}, duration)
}
}
}
// 注冊 toast 插件
Vue.use(ToastObj)
3. 在其他組件內(nèi)使用
Vue.component('my-button', {
data() {
return {
wrapperStyle: {
width: '70px',
padding: '20px',
backgroundColor: 'green'
},
textStyle: {
color: '#fff',
fontSize: '16px'
}
}
},
methods: {
handleClick() {
this.$toast({
message: '我點(diǎn)擊了'
})
}
},
template: `<div :style="wrapperStyle" @click="handleClick">
<span :style="textStyle">點(diǎn)擊提示</span>
</div>`
})
const vm = new Vue({
el: '#app'
})
</script>
</body>
</html>
總結(jié)
到此這篇關(guān)于Vue封裝全局toast組件的文章就介紹到這了,更多相關(guān)Vue封裝全局toast組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js 實(shí)現(xiàn)輸入框動態(tài)添加功能
這篇文章主要介紹了vue.js 實(shí)現(xiàn)輸入框動態(tài)添加功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件
這篇文章主要為大家介紹了基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue+element?分頁封裝的實(shí)現(xiàn)示例
本文主要介紹了vue+element?分頁封裝的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
element-ui配合node實(shí)現(xiàn)自定義上傳文件方式
這篇文章主要介紹了element-ui配合node實(shí)現(xiàn)自定義上傳文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

