模态框对象封装

目录结构

main.js

import plugins from './plugins'
Vue.use(plugins)

index.js

import modal from './modal'

export default {
  install(Vue) {
    /**
     * 同理,可在此配置自己想封装的其他全局模块功能
    */ 
    // 模态框对象
    Vue.prototype.$modal = modal
  }
}

model.js

import { Message, MessageBox, Notification, Loading } from 'element-ui'

let loadingInstance;

export default {
  // 消息提示
  msg(content) {
    Message.info(content)
  },
  // 错误消息
  msgError(content) {
    Message.error(content)
  },
  // 成功消息
  msgSuccess(content) {
    Message.success(content)
  },
  // 警告消息
  msgWarning(content) {
    Message.warning(content)
  },
  // 弹出提示
  alert(content) {
    MessageBox.alert(content, "系统提示")
  },
  // 错误提示
  alertError(content) {
    MessageBox.alert(content, "系统提示", { type: 'error' })
  },
  // 成功提示
  alertSuccess(content) {
    MessageBox.alert(content, "系统提示", { type: 'success' })
  },
  // 警告提示
  alertWarning(content) {
    MessageBox.alert(content, "系统提示", { type: 'warning' })
  },
  // 通知提示
  notify(content) {
    Notification.info(content)
  },
  // 错误通知
  notifyError(content) {
    Notification.error(content);
  },
  // 成功通知
  notifySuccess(content) {
    Notification.success(content)
  },
  // 警告通知
  notifyWarning(content) {
    Notification.warning(content)
  },
  // 确认窗体
  confirm(content) {
    return MessageBox.confirm(content, "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
  },
  // 提交内容
  prompt(content) {
    return MessageBox.prompt(content, "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
  },
  // 打开遮罩层
  loading(content) {
    loadingInstance = Loading.service({
      lock: true,
      text: content,
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
    })
  },
  // 关闭遮罩层
  closeLoading() {
    loadingInstance.close();
  }
}

使用

// 举例
this.$modal.msgError(“错误提示”)

this.$modal.loading("页面加载文字...")

this.$modal.closeLoading() // 关闭加载状态 

// 二次确认方法
this.$modal.confirm('是否确认删除文件名为"' + name + '"的数据项?').then(() => {
  return delete(id);
}).then(() => {
  this.getList();
  this.$modal.msgSuccess("删除成功");
}).catch(() => {});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Bootstrap模态框封装,你可以按照以下步骤进行操作: 1. 引入Bootstrap库:在你的HTML文件中,确保你已经引入了Bootstrap的CSS和JavaScript文件。可以使用CDN链接或者本地文件。 2. 创建模态框的HTML结构:在HTML文件中添加一个用于模态框的容器元素,例如一个`<div>`元素。给这个元素添加一个唯一的ID,以便在后续的操作中引用它。 3. 触发模态框的按钮或链接:在页面中添加一个触发模态框的按钮或链接。给这个按钮或链接添加一个`data-toggle`属性,并将其值设置为`modal`。然后,将`data-target`属性设置为步骤2中创建的模态框容器元素的ID。 4. 内容填充:在模态框容器元素中添加模态框的内容。这可以是文本、表单、图像等等。根据你的需求进行相应的布局和样式设置。 5. JavaScript交互:为了使模态框能够正常工作,你需要添加一些JavaScript代码。使用jQuery或者原生JavaScript来实现以下功能: - 当点击触发按钮时,显示模态框; - 当点击模态框外部区域或关闭按钮时,隐藏模态框; - 可以通过编写事件处理程序来处理模态框上的表单提交、按钮点击等交互操作。 以下是一个简单的示例代码,演示了如何封装Bootstrap模态框: ```html <!-- 步骤2:模态框的容器元素 --> <div id="myModal" class="modal"> <!-- 步骤4:模态框的内容 --> <div class="modal-content"> <span class="close">×</span> <h2>模态框标题</h2> <p>模态框内容</p> </div> </div> <!-- 步骤3:触发模态框的按钮 --> <button type="button" data-toggle="modal" data-target="#myModal">打开模态框</button> <!-- 步骤5:JavaScript交互 --> <script> // 当点击触发按钮时,显示模态框 $('[data-toggle="modal"]').on('click', function() { $('#myModal').show(); }); // 当点击关闭按钮或模态框外部区域时,隐藏模态框 $('.close, .modal').on('click', function() { $('#myModal').hide(); }); </script> ``` 以上是一个基本的封装示例,你可以根据你的实际需求进行修改和扩展。希望对你有帮助!如有更多问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值