1:下载json2csv插件
2:写一个方法 默认导出
import { Parser } from 'json2csv'
export const exportCSV = async (data = {}, fields = [], fileName) => {
try {
const parser = new Parser({ fields })
const result = parser.parse(data)
let uri = 'data:text/csv;charset=utf-8,\ufeff' + result
let link = document.createElement('a')
link.href = encodeURI(uri)
link.download = `${fileName}.csv`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch (err) {
alert(err)
}
}
2:在组件中使用
import { exportCSV } from '@/文件地址'
let fields = [
{
label: title1,
value: key名1
},
{
label:title2,
value: key名2
}
]
let fileName = 'list_' + Date.now()
exportCSV(数据的数组, fields, fileName)