1、弹框展示
2、message组件
新建一个message.vue
<template><div class="wrapper" v-if="isShow" :class="showContent ? 'fadein' : 'fadeout'">{{ text }}</div>
</template>
<script></script>
<style scoped>
.wrapper {width: 200px;text-align: center;position: fixed;left: 50%;top: 50px;background: rgb(237, 242, 252);padding: 10px;border-radius: 5px;border: 1px solid rgb(235, 238, 245);transform: translate(-50%, -50%);color: rgb(201, 173, 153);font-size: 28px !important;
}.fadein {animation: animate_in 1s;
}.fadeout {animation: animate_out 1s;opacity: 0;
}@keyframes animate_in {0% {opacity: 0;}100% {opacity: 1;}
}@keyframes animate_out {0% {opacity: 1;}100% {opacity: 0;}
}
</style>
index.js 跟message组件放在同一目录下
import vue from 'vue'
//引入组件
import messComponent from './message.vue'
//创建构造器
const MessConstructor = vue.extend(messComponent)
function showMessage(text) {const message = new MessConstructor({el: document.createElement('div'),data() {return {text: text,isShow: true, // 是否显示组件showContent: true // 作用:在隐藏组件之前,显示隐藏动画}}})document.body.appendChild(message.$el)setTimeout(() => { message.showContent = false }, 2000)// 过了 duration 时间隐藏组件setTimeout(() => { message.isShow = false }, 2000 * 2)
}function messageCom() {
//挂到vue上去,方便调用vue.prototype.$message = showMessage
}
export default messageCom
main.js 全局引入组件
import Vue from 'vue'
import App from './App'
import router from './router'
import messageCom from './message/index'
Vue.config.productionTip = false
Vue.use(messageCom)new Vue({el: '#app',router,components: { App },template: '<App/>'
})
调用
<template><div id="app"><input type="button" value="显示提示" @click="showMess"><!-- <router-view /> --></div>
</template><script>
import message from './message/message'
export default {name: 'App',components: { message },methods: {showMess() {this.$message("我是提示消息")}}
}
</script><style>
#app {margin-top: 100px;
}
</style>