elementUI的二次封装--提示框全局样式

1、起因

在开发中,我们通常使用UI库和自己写的组件。自己写的组件遇到需求变更了,那么,我们只要修改全局组件,就可以满足我们的需求了,但是,我们通常使用三方UI库,如果需求变更了,那么,UI库就从全局组件变成了私有组件了

 

2、解决方案

使用UI库前,封装为全局组件

即,使用UI库的组件前,自己写一个全局组件,里面直接放三方UI的组件,这样,如果发生全局样式更改,功能更改,就不会影响原来的业务逻辑,无痛过渡。

 

可能需要用到render函数

 

prototype实现

则,进行拦截处理,类似中间件概念

 

3、例子

全局组件的方式,下篇文章再进行举例子,这次例子为:prototype实现的二次封装,在不动UI库源码的情况下实现自己需要的需求

 

1、例子目标:Notification、Message

这两个是elementUI的弹框,但是,在实际使用中,这两个提示框经常被蒙层遮盖,由于elementUI中的z-index是动态计算的,没有时间研究源码,所以,我们得给这两个东西添加样式,查看官网提供的方法如下:

 

image.png

 

意味着我们每次调用的时候,都要多写这个参数,如果有其他更改,还得重新全局查找,替换,十分麻烦

 

2、改造--查看源码

import Vue from 'vue';
import Main from './main.vue';
import { PopupManager } from 'element-ui/src/utils/popup';
import { isVNode } from 'element-ui/src/utils/vdom';
let MessageConstructor = Vue.extend(Main);

let instance;
let instances = [];
let seed = 1;

const Message = function(options) {
  if (Vue.prototype.$isServer) return;
  options = options || {};
  if (typeof options === 'string') {
    options = {
      message: options
    };
  }
  let userOnClose = options.onClose;
  let id = 'message_' + seed++;

  options.onClose = function() {
    Message.close(id, userOnClose);
  };
  instance = new MessageConstructor({
    data: options
  });
  instance.id = id;
  if (isVNode(instance.message)) {
    instance.$slots.default = [instance.message];
    instance.message = null;
  }
  instance.$mount();
  document.body.appendChild(instance.$el);
  let verticalOffset = options.offset || 20;
  instances.forEach(item => {
    verticalOffset += item.$el.offsetHeight + 16;
  });
  instance.verticalOffset = verticalOffset;
  instance.visible = true;
  instance.$el.style.zIndex = PopupManager.nextZIndex();
  instances.push(instance);
  return instance;
};

['success', 'warning', 'info', 'error'].forEach(type => {
  Message[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options
      };
    }
    options.type = type;
    return Message(options);
  };
});

Message.close = function(id, userOnClose) {
  let len = instances.length;
  let index = -1;
  for (let i = 0; i < len; i++) {
    if (id === instances[i].id) {
      index = i;
      if (typeof userOnClose === 'function') {
        userOnClose(instances[i]);
      }
      instances.splice(i, 1);
      break;
    }
  }
  if (len <= 1 || index === -1 || index > instances.length - 1) return;
  const removedHeight = instances[index].$el.offsetHeight;
  for (let i = index; i < len - 1 ; i++) {
    let dom = instances[i].$el;
    dom.style['top'] =
      parseInt(dom.style['top'], 10) - removedHeight - 16 + 'px';
  }
};

Message.closeAll = function() {
  for (let i = instances.length - 1; i >= 0; i--) {
    instances[i].close();
  }
};

export default Message;

 

 

以上node_modules里面elementUI的代码,侵删!!!

 

3、实现过程

我们看到,最后是:export default Message;

我们引用的时候:

 

import Vue from 'vue';
import { Message } from 'element-ui';
Vue.prototype.$message = Message;

 

 

所以,我们只要在代码:Vue.prototype.$message = Message  的时候将Message替换为我们二次封装的就行了,而Notification源码和Message基本上一样的。那么,看核心代码:

 

// 封装默认class
const notify = option =>
  Notification(Object.assign({ customClass: 'notify_class' }, option))
const $message = option =>
  Message(Object.assign({ customClass: 'notify_class' }, option))
const optionsBox = (options, type) => {
  if (typeof options === 'string') {
    options = { message: options }
  }
  options.type = type
  return options
}
let typeArr = ['success', 'warning', 'info', 'error']
Object.keys(Notification).forEach(type => {
  if (typeArr.includes(type)) {
    notify[type] = options => notify(optionsBox(options, type))
    $message[type] = options => $message(optionsBox(options, type))
  } else {
    notify[type] = Notification[type]
    $message[type] = Message[type]
  }
})

Vue.prototype.$notify = notify
Vue.prototype.$message = $message

 

 

以上,就可以全局添加样式class了,代码量有点多,只为了添加一个class,感觉不划算,但是,这还可以继续添加其他的全局功能,不止class

posted @ 2019-12-17 11:21  白木兰  阅读(2462)  评论(0编辑  收藏  举报