视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
vue.js开发实现全局调用的MessageBox组件实例代码
2020-11-27 22:25:09 责编:小采
文档

前言

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

源码

github地址:Talk is cheap. Show me the code.

本地下载地址:http://xiazai.jb51.net/201711/yuanma/vue-messagebox(jb51.net).rar

组件模板

// /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(() => {
 // ...
}); 

最后来张效果图


总结

下载本文
显示全文
专题