按需加载调用一个前端组件

1.设计index.js入口

import Vue from 'vue'
// 直接将Vue组件作为Vue.extend的参数
import messageboxEvent from "./messageboxEvent";

const showMessageboxEvent = (data, config) => {
    let btnList = data.btnList || [];
    let modalText = data.modalText || null;
    let record = data.record || null;
    let otherObj = data.otherObj || {};
    config = config || {};
    const messageboxEventConstructor = Vue.extend(messageboxEvent);
    const instance = new messageboxEventConstructor({
        el: document.createElement("div")
    });

    document.querySelector("body").appendChild(instance.$el);
    return instance.show(btnList, modalText, record, otherObj, config);
}

export default showMessageboxEvent;

2.引入弹框组件vue

<template>
  <el-dialog v-if="popenevTVisible" :title="modalTitle" :before-close="handleCancel" :visible.sync="popenevTVisible" width="420px" :close-on-click-modal="false">
    <div class="message-tip">{{ modalText }}</div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button v-for="(btn, index) in btnList" :key="index" type="primary" @click="handleOk(btn)">{{ btn.btnTitle }}</el-button>

    </span>
  </el-dialog>
</template>

<script>
import { clickEvent } from "../../event/eventClick";
export default {
  name: "messageboxEvent",
  data() {
    return {
      popenevTVisible: true,
      btnList: [],
      modalText: null,
      modalTitle: null,
      promiseStatus: null,
      otherObj: {},
      record: null,
    };
  },
  methods: {
    show(btnList, modalText, record, otherObj, config) {
      this.btnList = btnList || [];
      console.log(this.btnList);
      this.modalText = modalText || "";
      this.modalTitle = config.modalTitle || "询问";
      this.otherObj = otherObj || {};
      this.record = record || null;
      // 创建promise
      return new Promise((resolve, reject) => {
        this.promiseStatus = {
          resolve,
          reject,
        };
      });
    },
    handleCancel() {
      this.promiseStatus.reject();
      this.destroyElement();
    },
    handleOk(cEvent) {
      clickEvent({ btnEvent: cEvent }, this.record, this.otherObj);
      this.promiseStatus.resolve();
      this.destroyElement();
    },
    /**
     * cs 2022-4-8
     * 销毁组件
     */
    destroyElement() {
      this.popenevTVisible = false;
      // this.$destroy(true);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  border-radius: 10px;
}

::v-deep .el-dialog__body {
  padding: 10px;
  text-align: center;
}

.message-tip {
  font-size: 14px;
  line-height: 24px;
}
</style>


3.全局注册方法,根据事件按需调用组件

import showMessageboxEvent from'../components/messageBoxEvent/index';

 *全局封装按需调用的事件*
 * @param {*} event 
 * @param {*} cEvent 
 * @param {*} record 
 * @param {*} otherObj 
 */
function messageboxEvent(event, cEvent, record, otherObj) {
    let btnList = [];
    for (let i = 0; i < cEvent.buttonList.length; i++) {
        cEvent.eventActions[i].btnTitle = cEvent.buttonList[i];
        btnList.push(cEvent.eventActions[i]);
    }

    showMessageboxEvent({
        modalText: cEvent.messageContant,
        btnList: btnList,
        record: record,
        otherObj: otherObj,
    }, {
        modalTitle: cEvent.title,
    }).then(res => {

    })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值