【前端】js读取资源目录下txt文件内容
import axios from 'axios'
getData() {
this.colorList = [];
axios.get(this.filePath).then(res => {
let resDataArr = res.data.split('\r\n');
//得到行数组
resDataArr.forEach(item => {
//对每一行操作
let rule = new RegExp(' ', 'g')
//将每行中空格替换为,
let cItem = item.replace(rule, ',');
let itemArr = cItem.split(',');
if (itemArr[0]) {
//排除txt文件最后的空行
this.colorList.push({
value: itemArr[0],
color: `rgba(${itemArr[1]},${itemArr[2]},${itemArr[3]},1)`
})
}
})
})
},