以前用js 封裝過一個小的提示元件, 現在用vue再次封裝下 檢視效果地址 文章載入完, 就可以看到
Alert.vue
<template>
<div class="box-alert" @click="hide">
<span>{{message + " "}} </span>
</div>
</template>
<script>
import $ from 'jquery'
export default{
data(){
return {
message: "操作成功",
flag: true
}
},
watch: {
visiable(val){
if(val)
this.show();
else
this.hide();
}
},
methods: {
show(){
let boxDom = $(".box-alert");
boxDom.animate({top: 0}, 709).delay(800).animate({top: -69});
if (this.flag) {
boxDom.css("background","#79ff79");
boxDom.find("i").removeClass().addClass("iconfont icon-success");
}
else {
boxDom.css("background","#ff7575");
boxDom.find("i").removeClass().addClass("iconfont icon-error");
}
},
hide(){
$(".box-alert").stop(true,true).animate({top:-69},600);
}
}
}
</script>
<style lang="less" scoped>
.box-alert {
height: 69px;
text-align: center;
color: white;
position: fixed;
font-size: 16px;
top: -69px;
left: 0px;
width: 100%;
z-index: 9999;
line-height: 60px;
background:#F44336;
}
</style>
複製程式碼
Alert.js
import alert from './Alert.vue'
let instance;
const plugin = {
install(vue) {
let Alerting = vue.extend(alert);
if(!instance){
instance = new Alerting({
el: document.createElement('div')
})
document.body.appendChild(instance.$el);
}
const alerting = {
show (options = {}){
if(typeof options === 'string'){
instance.message = options;
instance.flag = false;
}
else if(typeof options === 'object'){
instance.flag = options['flag'];
instance.message = options['text'];
}
instance.show();
}
}
vue.$xalert = alerting;
vue.mixin({
created: function(){
this.$xalert = vue.$xalert;
}
})
}
}
export default plugin
export const install = plugin.install
複製程式碼
在main.js 中全域性引入
import loadding from './components/owner/ownerAlert/Alert.js'
Vue.use(loadding);
複製程式碼
在其他頁面想要使用
this.$xalert.show({
text: "載入完成",
flag: true
});
複製程式碼