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

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 => {

    })
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现前端el-plan-tab随机加载组件,你可以按照以下步骤操作: 1. 在el-plan-tab组件中定义一个,用于存储要加载组件的名称或路径。 2. 使用JavaScript中的Math.random()方法生成一个随机数,得到要加载组件在数中的索引。 3. 根据随机索引获取要加载组件的名称或路径。 4. 使用Vue.js提供的异步组件实现动态加载组件,即使用<component :is="componentName"></component>标签动态加载组件。 下面是一个参考代码示例: ``` <template> <el-plan-tab> <component :is="componentName"></component> </el-plan-tab> </template> <script> export default { name: 'RandomComponentLoader', data() { return { components: ['ComponentA', 'ComponentB', 'ComponentC'], // 定义要加载组件名称 componentName: '', // 定义当前要加载组件名称 }; }, mounted() { this.loadRandomComponent(); }, methods: { loadRandomComponent() { const randomIndex = Math.floor(Math.random() * this.components.length); // 生成随机索引 this.componentName = this.components[randomIndex]; // 获取要加载组件名称 import(`@/components/${this.componentName}.vue`).then(() => { // 使用Vue.js提供的异步组件实现动态加载组件 console.log(`${this.componentName} loaded`); }); }, }, }; </script> ``` 在上面的代码示例中,我们定义了一个components数,其中存储了三个要加载组件名称。在mounted钩子函数中调用了loadRandomComponent方法,该方法使用Math.random()方法生成一个随机索引,并根据该索引获取要加载组件名称。然后,使用异步组件实现动态加载组件,并将组件名称赋值给componentName变量,从而实现随机加载组件的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值