全局挂载
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// 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)
}
}