一、新建公共函数 common.js
import xlsx from 'xlsx'
function readExcelFile (url) {
this.$http.get(url, { responseType: 'arraybuffer' })
.then((res) => {
const data = new Uint8Array(res.data)
const wb = xlsx.read(data, { type: 'array' })
const sheets = wb.Sheets
this.content = transformSheets(sheets)
}).catch(err => {
this.err = err
})
}
function transformSheets (sheets) {
const content1 = []
const tmplist = []
for (const key in sheets) {
tmplist.push(xlsx.utils.sheet_to_json(sheets[key]).length)
content1.push(xlsx.utils.sheet_to_json(sheets[key]))
}
console.log(tmplist)
console.log(content1)
}
export { readExcelFile }
二、main.js 中引入公共函数
import { readExcelFile } from './common.js'
Vue.prototype.$readExcelFile = readExcelFile
三、组件中使用公共函数
this.$readExcelFile('/systemFiles/menu.xlsx')