Vue全局过滤器(常用的):
1、时间过滤器
import Vue from 'vue' import moment from 'moment' import 'moment/locale/zh-cn' moment.locale('zh-cn')
Vue.filter('dateFilter', function (dateStr, pattern = 'YYYY-MM-DD HH:mm:ss') { return moment(dateStr).format(pattern) })
|
2、整数逢三断一
- 只有全部都是整数才会以逗号分隔去呈现
- 如果有小数点则会没有效果
Vue.filter('numberFormat', function (val) { if (!val) { return '0' } const intPartFormat = val.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') return intPartFormat })
|
3、二进制流文件下载
import axios from '@/utils/request'
export function fileExport() { return axios({ url: '/file/download', method: 'get', responseType: 'blob' }) }
|
- 编写一个公共方法,然后引入写好的文件接口
- 后端返回一个二进制流或者文件流,和文件类型
- 如果有文件名则去出文件名,如果没有自己可以自定义文件名字,以下是后端把文件名存到了请求头
headers
里面,所以就从headers
里取出文件名 attachId
是文件id,一般每一个文件都会有一个id
作为标识
import { fileExport } from '../services/test'
export function downloadFile(attachId) { fileExport(attachId).then(res => { const blob = new Blob([res.data], { type: res.data.type }) const url = window.URL.createObjectURL(blob) let filename = res.headers[' content -disposition'].split('filename=')[1] const link = document.createElement('a') link.setAttribute('style', 'display: none ') link.setAttribute('href', url) link.setAttribute(' download', filename) document.body.appendChild(link) link.click() document.body.removeChild(link) }) }
|
温馨提示:以上方法仅供参考!博主只是想记录一下,方便以后回顾。