vue 全局message 提醒 toast 全局组件
1,创建文件夹alert 在文件夹内创建vue文件 toast.vue<template><transition name="alert-fade"><div id="toast"v-show="visible"class="dialog-tips dialog-center">{{me...
·
1,创建文件夹alert 在文件夹内创建vue文件 toast.vue
<template>
<transition name="alert-fade">
<div id="toast"
v-show="visible"
class="dialog-tips dialog-center">
{{message}}
</div>
</transition>
</template>
<script>
export default {
data () {
return {
visible: false,
message: ''
}
}
}
</script>
<style lang="less" scoped>
.alert-fade-enter-active,
.alert-fade-leave-active {
transition: opacity 0.3s;
}
.alert-fade-enter,
.alert-fade-leave-to {
opacity: 0;
}
.dialog-tips {
position: fixed;
z-index: 100;
min-width: 220px;
padding: 30px;
border-radius: 15px;
white-space: nowrap;
background-color: #fff;
box-shadow: 0px 8px 30px 0 rgba(0, 0, 0, 0.363);
text-align: center;
color: rgb(70, 70, 70);
}
.dialog-center {
top: 20%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
2,创建index.js
import toastcom from './toast.vue'
const toast = {}
toast.install = vue => {
const ToastCon = vue.extend(toastcom)
const ins = new ToastCon()
ins.$mount(document.createElement('div'))
document.body.appendChild(ins.$el)
vue.prototype.$toast = (msg, duration = 3000) => {
ins.message = msg
ins.visible = true
setTimeout(() => {
ins.visible = false
}, duration)
}
}
export default toast
3,安装
main.js
import toast from './alert/index'
Vue.use(toast)
4,使用
this.$toast('欢迎')
更多推荐
已为社区贡献23条内容
所有评论(0)