<template lang="html"> <transition name="message-fade"> <div class="wui__message" :class="[prefixCls + '--' + type]" v-if="visible"> <i :class="[prefixCls + '__icon' + '--' + type,iconType]"></i> <div :class="[prefixCls + '__content']"> <span :class="[prefixCls + '__content' + '__body']" v-html="content"></span> <span v-if="closeTxt" @click="close()" :class="[prefixCls + '__closeTxt']"> {{closeTxt}} </span> <span v-if="closable" :class="[prefixCls + '__iconbox']"> <i class="icon-close" :class="[prefixCls + '__icon']" @click="close()"></i> </span> </div> </div> </transition> </template> <script> const prefixCls = 'wui__message'; export default { name:'Message', data(){ return { prefixCls:prefixCls, } }, mounted() { //do something after mounting vue instance if(this.closeTxt){ this.closable = false } }, methods: { close() { this.visible = false } } } </script> <style lang="scss" scoped> </style>
import Vue from 'vue'; import Message from './src/message.vue'; const defaults = { visible:false, content:undefined, duration:'3', type:'info', closable:false, closeTxt:null, top:20, iconType:'' }; const typeMap = { "info":'icon-info', "error":'icon-heart-broken', "warning":'icon-stopwatch', "success":'icon-checkmark-outline' } const MessageVueConstructor = Vue.extend(Message); MessageVueConstructor.prototype.close = function() { var vm=this; this.$on('after-leave', _ => { if (vm.$el && vm.$el.parentNode) { vm.$el.parentNode.removeChild(vm.$el); } this.$destroy(); }); vm.visible = false; }; const messageBox = (options = {}) => { if (Vue.prototype.$isServer) return; options = Object.assign({}, defaults, options); let instance = new MessageVueConstructor({ el: document.createElement('div'), data: options }); if(!instance.type||!instance.content){return false} document.body.appendChild(instance.$el); Vue.nextTick(() => { instance.visible = true; instance.iconType = typeMap[instance.type] // if duration is 0 means can't close if(instance.duration!=0){ setTimeout(function(){ instance.close(); },options.duration*1000) } }); return instance; }; export default messageBox;
import WButton from './button/index.js'; import WMessage from './message/index'; const components = [ WButton, WMessage ] const install = function(Vue) { if (install.installed) return components.map(component => Vue.component(component.name, component)) Vue.prototype.$Message = WMessage; //important } if (typeof window !== 'undefined' && window.Vue) { install(window.Vue) } export default { install, WButton, WMessage, }
接下來就能夠愉快的使用了,具體的使用姿式以下css
this.$Message({content:'this is info'}) //不指定type默認爲info this.$Message({type:'info',content:'This is a closed message',closable:true}) //這種狀況就會有關閉的叉叉 this.$Message({type:'info',content:'this is 10-second message',duration:10}) //時長10秒讓message多待會 this.$Message({type:'info',content:'This is a message that can not be closed',duration:0}) //時長爲0表示不想讓消失 this.$Message({type:'info',content:'This is a custom closed text message',closeTxt:'朕知道了',duration:10}) //自定義關閉文字也能夠皮一下
至此一個message組件就愉快的實現了,能夠知足大多數場景的需求,擴展性也是比較OK的,但是總以爲還缺點功能,路過的看官伸出無敵的小手指正一下可好?html
下一篇vue
從零開始構建本身的vue組件庫番外篇
傳送門git
message組件源碼傳送門