從零開始構建本身的vue組件庫之——message篇

第二篇:從零開始構建本身的vue組件庫之————message組件

組件需求

  • message這種組件應該是按需加載的沒差了,用的時候就不要寫在頁面裏了,必須得是this.$message(config)這樣子用的,姿式很優美[滑稽];
  • 頂部居中吧,既然人家各類框架也是頂部居中,咱也隨大流啦,重在理解實現方式啦[再次滑稽];
  • 好了,具體需求是,用不一樣圖標表示不一樣類型的message(有默認);時長自定義或默認;內容自定義;可關閉或不可關閉(或點擊文字關閉);

PS:組件結構參考上一篇button組件

message.vue

<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>

message 目錄下的 index.js

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;

package下的index.js(繼上篇增長)

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組件源碼傳送門
相關文章
相關標籤/搜索