Vue 全局挂载

全局挂载

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// main.js全局挂载
import * as ENV from '@/pages/index/Env.js' // 常量
import * as FN from '@/pages/index/Fn.js'	// 方法
import Pagination from "@/src/components/Pagination"	// 组件
Vue.prototype.ENV = ENV
Vue.prototype.FN = FN
Vue.component('Pagination', Pagination)
// Env.js
// 文件类型
export const fileTypeOptions = [
    { value: "word", label: "word" },
    { value: "pdf", label: "pdf" },
]
// Fn.js
export function test() {
    console.log("挂载正常")
}

/**
 * 新页面预览pdf
 * @param {Blob} value 
 * @returns 
 */
export function previewPdf(res) {
    const binaryData = []
    binaryData.push(res)
    let pdfUrl = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }))
    window.open(pdfUrl)
}

/**
 * 下载
 * auto 跟随后台返回值设定文件名称
 * @param {*} value 
 * @param {*} arr 
 * @returns 
 */
export function download(res, name = '测试', suffix = 'txt', type = 'auto') {
    let nameH = name + '.' + suffix
    let content = res
    if (res.headers) {
        content = res.data
        if (type == 'auto') { // 跟随后台
            let str = res.headers['content-disposition']
            let index = str.lastIndexOf('=') + 1
            nameH = decodeURI(str.slice(index, str.length))
        }
    }
    const blob = new Blob([content]);
    const fileName = nameH
    if ("download" in document.createElement("a")) { // 判断ie浏览器
        const elink = document.createElement("a")
        elink.download = fileName
        elink.style.display = "none"
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        setTimeout(() => { // 宏任务打断清除缓存
            URL.revokeObjectURL(elink.href)
            document.body.removeChild(elink)
        }, 500)
    } else {
        navigator.msSaveBlob(blob, fileName)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

公众号:猫脸码客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值