国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答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開發(fā)實現全局調用的MessageBox組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧,希望能幫助到大家。組件模板// /src/component
推薦度:
導讀vue.js實現全局調用MessageBox組件:Vue.js組件知識點挺多的,而且很重要,本文就給大家介紹關于利用vue.js開發(fā)實現全局調用的MessageBox組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧,希望能幫助到大家。組件模板// /src/component
Vue.js組件知識點挺多的,而且很重要,本文就給大家介紹關于利用vue.js開發(fā)實現全局調用的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狀態(tài)
 confirm: function () {
 this.isShowMessageBox = false;
 if (this.isShowInput) {
 this.resolve(this.inputValue);
 } else {
 this.resolve('confirm');
 }
 this.remove();
 },
 // 取消,將promise斷定為reject狀態(tài)
 cancel: function () {
 this.isShowMessageBox = false;
 this.reject('cancel');
 this.remove();
 },
 // 彈出messageBox,并創(chuàng)建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官方文檔中有開發(fā)插件的介紹。具體實現代碼如下:

// /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: '請?zhí)顚懛诸惷Q',
 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開發(fā)實現全局調用的MessageBox組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧,希望能幫助到大家。組件模板// /src/component
推薦度:
標簽: VUE 實現 js
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 在线视频日韩 | 亚洲v欧美v日韩v国产v | 亚洲欧洲精品一区二区三区 | 二区在线播放 | 午夜国产在线视频 | 久久精品国产亚洲精品2020 | 欧美日韩亚洲色图 | 精品久久久久久亚洲 | 亚洲欧美日韩在线观看 | 中日韩在线 | 久久免费看视频 | 精品欧美一区二区三区在线 | 国产精品特级毛片一区二区三区 | 卡通动漫第一页 | 欧美成人日韩 | 欧美另类精品一区二区三区 | 国产精品久久成人影院 | 日韩免费一区二区三区在线 | 国产日韩欧美中文 | 欧美综合第一页 | 97国产精品欧美一区二区三区 | 最新亚洲精品 | 全部费免一级毛片不收费 | 精品一成人岛国片在线观看 | 日韩v欧美 | 一区二区三区成人 | 亚洲欧美日韩高清一区二区一 | 国产高清视频免费 | 啪网站| 欧美日韩午夜 | 国产成人精品日本亚洲11 | 亚洲国产天堂久久九九九 | 99久久亚洲精品影院 | 国产观看在线 | 久久久久无码国产精品一区 | 欧美国产精品va在线观看 | 视频二区 素人 欧美 日韩 | 国产高清在线精品一区二区三区 | 成人a毛片久久免费播放 | 亚洲第一区视频 | 日本美女逼逼 |