好久没来写东西了,主要是一直在加班,哼哼,不开心
项目中会用到将xml文件解析成json文件在页面中显示出来,以前jq的时候用到的方法行不通了,故在这边介绍一种我觉得还不错的插件
1. npm安装
npm i x2js
2.在main.js中引用
import x2jsfrom'x2js'//xml数据处理插件Vue.prototype.$x2js =newx2js()//创建x2js对象,挂到vue原型上
3.在组件中使用
代码如下:
methods: {
async Receivingdatainfo(){
let url = '/api/T20WebService.asmx/Get_CusMst';
let dat = await this.$axios.get(url);
let strindat = this.$x2js.xml2js(dat)
let msg = JSON.parse(strindat.string.__text)
if(msg.ok == true){
this.get_Fidarstprt = await msg.data;
this.Scanreceiptstor= Object.assign({},msg.data[0])
console.log(this.Scanreceiptstor);
}
},
注意:你的xml文件如果是放在前端这边的话,要把文件放到public文件夹当中,否则文件将会报404的错