最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3自定義提示框和下載方式

 更新時間:2026年02月24日 08:57:07   作者:oxygen-1204  
文章介紹了如何在JavaScript中修改`confirmInstall.js`和`MessageBox`組件,以接受并使用`confirmButtonText`參數來顯示自定義的確認按鈕,通過這些修改,可以使用`proxy.$ftConfirm`方法來傳遞自定義文本并處理確認按鈕點擊事件

1.confirmInstall.js

首先,確保 proxy.$ftConfirm 方法能夠接受 confirmButtonText 參數,并將其傳遞給 MessageBox 組件。

confirmInstall.js

import { render, createVNode, ref, reactive } from 'vue'
import confirm from '@/components/MessageBox'

export default {
  install: (app) => {
    app.config.globalProperties.$ftConfirm = (options) => {
      const show = ref(true)
      const wrapper = document.createElement('div')
      wrapper.id = 'ft-confirm'; // 創(chuàng)建一個id=ft-confirm的容器
      let vnode = createVNode(confirm); // 創(chuàng)建一個vnode
      const close = () => {
        // 關閉彈窗移除標簽
        document.body.removeChild(document.getElementById('ft-confirm'));
      }
      const dialogProps = reactive({
        onConfirm: options.onConfirm ? () => { options.onConfirm?.(); close() } : close,
        onCancel: close,
        type: options.type,
        message: options.message,
        tips: options.tips ? options.tips : '',
        show: show.value,
        confirmButtonText: options.confirmButtonText ? options.confirmButtonText : '確定', // 添加 confirmButtonText 參數
      });
      vnode = createVNode(confirm, dialogProps);
      render(vnode, wrapper); // 渲染組件
      document.body.appendChild(wrapper); // 添加到body
    }
  }
}

2. 修改MessageBox組件

確保 MessageBox 組件能夠接收并使用 confirmButtonText 參數來顯示確認按鈕。

假設MessageBox組件的結構如下

  • vue
<template>
  <div v-if="show" class="message-box">
    <div class="message-box-content">
      <div class="message-box-header">
        <span class="message-box-title">{{ type }}</span>
      </div>
      <div class="message-box-message">{{ message }}</div>
      <div class="message-box-tips" v-if="tips">{{ tips }}</div>
      <div class="message-box-buttons">
        <el-button type="primary" @click="handleConfirm">{{ confirmButtonText }}</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    onConfirm: {
      type: Function,
      default: () => {}
    },
    onCancel: {
      type: Function,
      default: () => {}
    },
    type: {
      type: String,
      default: 'info'
    },
    message: {
      type: String,
      required: true
    },
    tips: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    },
    confirmButtonText: {
      type: String,
      default: '確定'
    }
  },
  methods: {
    handleConfirm() {
      this.onConfirm();
    },
    handleCancel() {
      this.onCancel();
    }
  }
}
</script>

<style scoped>
.message-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.message-box-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  text-align: center;
}

.message-box-header {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.message-box-message {
  font-size: 16px;
  margin-bottom: 10px;
}

.message-box-tips {
  font-size: 14px;
  color: #999;
  margin-bottom: 20px;
}

.message-box-buttons {
  display: flex;
  justify-content: space-around;
}
</style>

3. 使用proxy.$ftConfirm方法

現在你可以使用 proxy.$ftConfirm 方法,并傳遞 confirmButtonText 參數來顯示自定義的確認按鈕。

示例代碼

proxy.$ftConfirm({
    type: 'success',
    message: '修改成功',
    confirmButtonText: '確定', // 設置確認按鈕的文本
    onConfirm: () => {
        // 跳轉到指定頁面
        router.push({ name: '目標頁面名稱' });
    },
    onCancel: () => {
        // 取消操作(如果需要)
    }
});

總結

1.修改 confirmInstall.js

  • 添加 confirmButtonText 參數到 dialogProps 中,并傳遞給 MessageBox 組件。

2.修改 MessageBox 組件

  • 添加 confirmButtonText 屬性,并在模板中使用該屬性來顯示確認按鈕。
  • 確保 handleConfirm 方法調用 onConfirm 回調。

3.使用 proxy.$ftConfirm 方法

  • 傳遞 confirmButtonText 參數來設置確認按鈕的文本。
  • 使用 onConfirm 回調來處理確認按鈕點擊后的邏輯。

通過這些調整,你可以在 proxy.$ftConfirm 方法中添加一個確認按鈕,并在點擊確認按鈕時執(zhí)行相應的操作。

用element-plus的消息框

  • 在src下的index.js
import tab from './tab'
import auth from './auth'
import cache from './cache'
import modal from './modal'
import download from './download'

export default function installPlugins(app){
  // 頁簽操作
  app.config.globalProperties.$tab = tab
  // 認證對象
  app.config.globalProperties.$auth = auth
  // 緩存對象
  app.config.globalProperties.$cache = cache
  // 模態(tài)框對象
  app.config.globalProperties.$modal = modal
  // 下載文件
  app.config.globalProperties.$download = download
}
  • modal.js
import { ElMessage, ElMessageBox, ElNotification, ElLoading } from 'element-plus'

let loadingInstance;

export default {
  // 消息提示
  msg(content) {
    ElMessage.info(content)
  },
  // 錯誤消息
  msgError(content) {
    ElMessage.error(content)
  },
  // 成功消息
  msgSuccess(content) {
    ElMessage.success(content)
  },
  // 警告消息
  msgWarning(content) {
    ElMessage.warning(content)
  },
  // 彈出提示
  alert(content) {
    ElMessageBox.alert(content, "系統提示")
  },
  // 錯誤提示
  alertError(content) {
    ElMessageBox.alert(content, "系統提示", { type: 'error' })
  },
  // 成功提示
  alertSuccess(content) {
    ElMessageBox.alert(content, "系統提示", { type: 'success' })
  },
  // 警告提示
  alertWarning(content) {
    ElMessageBox.alert(content, "系統提示", { type: 'warning' })
  },
  // 通知提示
  notify(content) {
    ElNotification.info(content)
  },
  // 錯誤通知
  notifyError(content) {
    ElNotification.error(content);
  },
  // 成功通知
  notifySuccess(content) {
    ElNotification.success(content)
  },
  // 警告通知
  notifyWarning(content) {
    ElNotification.warning(content)
  },
  // 確認窗體
  confirm(content) {
    return ElMessageBox.confirm(content, "系統提示", {
      confirmButtonText: '確定',
      cancelButtonText: '取消',
      type: "warning",
    })
  },
  // 提交內容
  prompt(content) {
    return ElMessageBox.prompt(content, "系統提示", {
      confirmButtonText: '確定',
      cancelButtonText: '取消',
      type: "warning",
    })
  },
  // 打開遮罩層
  loading(content) {
    loadingInstance = ElLoading.service({
      lock: true,
      text: content,
      background: "rgba(0, 0, 0, 0.7)",
    })
  },
  // 關閉遮罩層
  closeLoading() {
    loadingInstance.close();
  }
}
  • download.js
import axios from 'axios'
import { ElLoading, ElMessage } from 'element-plus'
import { saveAs } from 'file-saver'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { blobValidate } from '@/utils/ruoyi'

const baseURL = import.meta.env.VITE_APP_BASE_API
let downloadLoadingInstance;

export default {
  name(name, isDelete = true) {
    var url = baseURL + "/common/download?fileName=" + encodeURIComponent(name) + "&delete=" + isDelete
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then((res) => {
      const isBlob = blobValidate(res.data);
      if (isBlob) {
        const blob = new Blob([res.data])
        this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
      } else {
        this.printErrMsg(res.data);
      }
    })
  },
  resource(resource) {
    var url = baseURL + "/common/download/resource?resource=" + encodeURIComponent(resource);
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then((res) => {
      const isBlob = blobValidate(res.data);
      if (isBlob) {
        const blob = new Blob([res.data])
        this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
      } else {
        this.printErrMsg(res.data);
      }
    })
  },
  zip(url, name) {
    var url = baseURL + url
    downloadLoadingInstance = ElLoading.service({ text: "正在下載數據,請稍候", background: "rgba(0, 0, 0, 0.7)", })
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then((res) => {
      const isBlob = blobValidate(res.data);
      if (isBlob) {
        const blob = new Blob([res.data], { type: 'application/zip' })
        this.saveAs(blob, name)
      } else {
        this.printErrMsg(res.data);
      }
      downloadLoadingInstance.close();
    }).catch((r) => {
      console.error(r)
      ElMessage.error('下載文件出現錯誤,請聯系管理員!')
      downloadLoadingInstance.close();
    })
  },
  saveAs(text, name, opts) {
    saveAs(text, name, opts);
  },
  async printErrMsg(data) {
    const resText = await data.text();
    const rspObj = JSON.parse(resText);
    const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
    ElMessage.error(errMsg);
  }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • vue中radio單選框如何實現取消選中狀態(tài)問題

    vue中radio單選框如何實現取消選中狀態(tài)問題

    這篇文章主要介紹了vue中radio單選框如何實現取消選中狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解Vue 全局引入bass.scss 處理方案

    詳解Vue 全局引入bass.scss 處理方案

    本篇文章主要介紹了詳解Vue 全局引入bass.scss 處理方案,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue-cli + sass 的正確打開方式圖文詳解

    vue-cli + sass 的正確打開方式圖文詳解

    本文通過圖文并茂的形式給大家介紹了vue-cli + sass 的正確打開方式,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-10-10
  • vue?動態(tài)添加el-input的實現邏輯

    vue?動態(tài)添加el-input的實現邏輯

    這篇文章主要介紹了vue?動態(tài)添加el-input的實現代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • vue計算屬性computed的使用方法示例

    vue計算屬性computed的使用方法示例

    這篇文章主要介紹了vue計算屬性computed的使用方法,結合實例形式分析了vue計算屬性computed的基本用法及相關操作注意事項,需要的朋友可以參考下
    2019-03-03
  • vue computed的傳參和element-plus彈窗的顯示過程

    vue computed的傳參和element-plus彈窗的顯示過程

    這篇文章主要介紹了vue computed的傳參和element-plus彈窗的顯示過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • Vue body樣式修改方式

    Vue body樣式修改方式

    這篇文章主要介紹了Vue body樣式修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue3新特性與最佳實踐總結與開發(fā)技巧

    Vue3新特性與最佳實踐總結與開發(fā)技巧

    在Vue3中性能優(yōu)化是提升用戶體驗的關鍵,通過合理地利用Vue3的新特性和優(yōu)化策略,可以顯著提升應用的加載速度和運行效率,這篇文章主要介紹了Vue3新特性與最佳實踐總結與開發(fā)技巧的相關資料,需要的朋友可以參考下
    2025-11-11
  • vue之原生上傳圖片加水印并壓縮圖片大小方式

    vue之原生上傳圖片加水印并壓縮圖片大小方式

    文章介紹了如何使用Vue原生上傳圖片,并在圖片上添加水印并壓縮圖片大小,首先,安裝了相應的插件并進行封裝,然后,介紹了添加水印的方法和上傳圖片的過程,最后,作者分享了自己的經驗,并希望對大家有所幫助
    2025-01-01
  • element-ui?table表格控件實現單選功能代碼實例

    element-ui?table表格控件實現單選功能代碼實例

    這篇文章主要給大家介紹了關于element-ui?table表格控件實現單選功能的相關資料,單選框是指在?Element?UI?的表格組件中,可以通過單選框來選擇一行數據。用戶只能選擇一行數據,而不能同時選擇多行,需要的朋友可以參考下
    2023-09-09

最新評論

伊春市| 依兰县| 柳河县| 梨树县| 齐河县| 南宁市| 祁东县| 巨野县| 佛山市| 阿鲁科尔沁旗| 九龙坡区| 隆德县| 宁陕县| 岚皋县| 凤凰县| 博爱县| 连南| 昂仁县| 石屏县| 揭阳市| 襄垣县| 博兴县| 渭南市| 枣强县| 普陀区| 德江县| 蛟河市| 云阳县| 循化| 靖边县| 盖州市| 丰顺县| 道孚县| 疏勒县| 花莲市| 固镇县| 双峰县| 蒙阴县| 西林县| 德格县| 临桂县|