项目作者: liuboshuo

项目描述 :
vue的toast和loading的插件
高级语言: Vue
项目地址: git://github.com/liuboshuo/vue-toast-indicator.git
创建时间: 2018-11-24T05:48:55Z
项目社区:https://github.com/liuboshuo/vue-toast-indicator

开源协议:MIT License

下载


vue的toast和loading组件

前言

我们在项目中都会用到toast,loading加载器。特别是在进行移动端的项目开发的时候,
进行异步操作的时候去等待,在必要的时候用toast给用户提示。在app开发中,iOS有成熟的MBProgressHUD插件等等。
在web前端也有很多第三方库也提供了这些组件,比如mint-ui,element-ui等都包含这些组件,
但是存在一个缺点,当我们只需要使用toast,loading组件,并用不到其他组件的时候,我们还是需要安装整个组件库,
会导致我们依赖的第三方库增大。所以我就做了一个轻量级的toast,loading插件vue-toast-indicator。

组件的使用

导入包,全局使用

  1. import {
  2. Toast,
  3. Indicator
  4. } from './vue-toast-indicator'
  5. Vue.use(Toast);
  6. Vue.use(Indicator);

toast的使用

  1. this.$Toast({message:"toast...",position:"bottom"})

indicator使用

  1. 加载
  2. this.$Indicator({message:"加载中..."})
  3. 消失
  4. setTimeout(()=>{
  5. this.$Indicator.hidden();
  6. },600);

组件的原理

封装一个vue组件,创建vue组件,然后动态插入到body节点

举个栗子:

  1. <template>
  2. <div>
  3. hello 小猿
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'toast',
  9. }
  10. </script>
  11. <style>
  12. </style>

上面创建了一个最简单的vue组件

假设我们导入该组件名字是HelloWorld

  1. // 构造组件
  2. const HelloWorldConstructor = Vue.extend(HelloWorld);
  3. export default {
  4. install(Vue,opt){
  5. Vue.prototype.$component = function(options = {}) {
  6. let parentNode = document.createElement("div");
  7. let instance = new ToastConstructor().$mount(parentNode);
  8. document.body.appendChild(instance.$el); // 动态插入
  9. return instance;
  10. }
  11. }
  12. }

就是这么简单,上面用到了vue的插件,自定义vue插件需要导出一个install方法

使用就更简单了

  1. import xxx from 'my-lib'
  2. Vue.use(xxx);

在项目中的任意vue组件可以通过this.$component();使用了