最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

vue.js实现全局调用MessageBox组件

来源:懂视网 责编:小采 时间:2020-11-27 20:10:19
文档

vue.js实现全局调用MessageBox组件

vue.js实现全局调用MessageBox组件:Vue.js组件知识点挺多的,而且很重要,本文就给大家介绍关于利用vue.js开发实现全局调用的MessageBox组件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧,希望能帮助到大家。组件模板// /src/component
推荐度:
导读vue.js实现全局调用MessageBox组件:Vue.js组件知识点挺多的,而且很重要,本文就给大家介绍关于利用vue.js开发实现全局调用的MessageBox组件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧,希望能帮助到大家。组件模板// /src/component
Vue.js组件知识点挺多的,而且很重要,本文就给大家介绍关于利用vue.js开发实现全局调用的MessageBox组件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧,希望能帮助到大家。

组件模板

// /src/components/MessageBox/index.vue
<template>
 <p class="message-box" v-show="isShowMessageBox">
 <p class="mask" @click="cancel"></p>
 <p 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>
 <p>
 <input type="text" v-model="inputValue" v-if="isShowInput" ref="input">
 </p>
 <p 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>
 </p>
 </p>
 </p>
 </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(() => {
 // ...
});

最后来张效果图


希望大家对Vue.js组件知识由一个更清晰的掌握,大家赶紧动手操作一下吧。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

vue.js实现全局调用MessageBox组件

vue.js实现全局调用MessageBox组件:Vue.js组件知识点挺多的,而且很重要,本文就给大家介绍关于利用vue.js开发实现全局调用的MessageBox组件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧,希望能帮助到大家。组件模板// /src/component
推荐度:
标签: VUE 实现 js
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top