当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue.js开发实现全局调用的MessageBox组件实例代码

vue.js开发实现全局调用的MessageBox组件实例代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

前言

一开始接触到vue中的组件的时候,对于组件的理解还是不够充分的,最近在开发个人博客项目中,一开始就没准备使用一些现在比较流行的ui库(毕竟是个人项目,多练练手还是好的),所以需要自己开发几个全局组件,这里以messagebox为例记录下vue.js如何开发全局组件。所谓全局变量是针对vue实例下说的,即所有的vue实际都可以运用到这个组件,局部组件就是针对某个实例来说的,只有这个vue实例下才能发挥作用,下面话不多说了,来一看看详细的介绍吧。

源码

github地址:talk is cheap. show me the code.

本地下载地址:

组件模板

// /src/components/messagebox/index.vue
<template>
 <div class="message-box" v-show="isshowmessagebox">
  <div class="mask" @click="cancel"></div>
  <div class="message-content">
  <svg class="icon" aria-hidden="true" @click="cancel">
   <use xlink:href="#icon-delete" rel="external nofollow" ></use>
  </svg>
   <h3 class="title">{{ title }}</h3>
   <p class="content">{{ content }}</p>
  <div>
   <input type="text" v-model="inputvalue" v-if="isshowinput" ref="input">
  </div>
   <div class="btn-group">
    <button class="btn-default" @click="cancel" v-show="isshowcancelbtn">{{ cancelbtntext }}</button>
    <button class="btn-primary btn-confirm" @click="confirm" v-show="isshowconfimrbtn">{{ confirmbtntext }}</button>
   </div>
  </div>
 </div>
 </template>
 
 <script>
 export default {
  props: {
  title: {
   type: string,
   default: '标题'
  },
  content: {
   type: string,
   default: '这是弹框内容'
  },
  isshowinput: false,
  inputvalue: '',
  isshowcancelbtn: {
   type: boolean,
   default: true
  },
  isshowconfimrbtn: {
   type: boolean,
   default: true
  },
  cancelbtntext: {
   type: string,
   default: '取消'
  },
  confirmbtntext: {
   type: string,
   default: '确定'
  }
  },
  data () {
  return {
   isshowmessagebox: false,
   resolve: '',
   reject: '',
   promise: '' // 保存promise对象
  };
  },
  methods: {
  // 确定,将promise断定为resolve状态
  confirm: function () {
   this.isshowmessagebox = false;
   if (this.isshowinput) {
   this.resolve(this.inputvalue);
   } else {
   this.resolve('confirm');
   }
   this.remove();
  },
  // 取消,将promise断定为reject状态
  cancel: function () {
   this.isshowmessagebox = false;
   this.reject('cancel');
   this.remove();
  },
  // 弹出messagebox,并创建promise对象
  showmsgbox: function () {
   this.isshowmessagebox = true;
   this.promise = new promise((resolve, reject) => {
   this.resolve = resolve;
   this.reject = reject;
   });
   // 返回promise对象
   return this.promise;
  },
  remove: function () {
   settimeout(() => {
   this.destroy();
   }, 300);
  },
  destroy: function () {
   this.$destroy();
   document.body.removechild(this.$el);
  }
  }
 };
 </script>
 <style lang="scss" scoped>
 // 此处省略 ...
 </style>

给组件添加全局功能

vue.js官方文档中有。具体实现代码如下:

// /src/components/messagebox/index.js

import msgboxvue from './index.vue'; 
// 定义插件对象
const messagebox = {};
// vue的install方法,用于定义vue插件
messagebox.install = function (vue, options) {
 const messageboxinstance = vue.extend(msgboxvue);
 let currentmsg, instance;
 const initinstance = () => {
 // 实例化vue实例
 currentmsg = new messageboxinstance();
 let msgboxel = currentmsg.$mount().$el;
 document.body.appendchild(msgboxel);
 };
 // 在vue的原型上添加实例方法,以全局调用
 vue.prototype.$msgbox = {
 showmsgbox (options) {
  if (!instance) {
  initinstance();
  }
  if (typeof options === 'string') {
  currentmsg.content = options;
  } else if (typeof options === 'object') {
  object.assign(currentmsg, options);
  }
  return currentmsg.showmsgbox();
 }
 };
};
export default messagebox;

全局使用

// src/main.js
import messagebox from './components/messagebox/index';
vue.use(messagebox);

页面调用

按照之前定义好的方法,可以在各个页面中愉快的调用该组件了。

this.$msgbox.showmsgbox({
 title: '添加分类',
 content: '请填写分类名称',
 isshowinput: true
}).then(async (val) => {
 // ...  
}).catch(() => {
 // ...
}); 

最后来张效果图


总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网