好久沒來寫東西了,主要是一直在加班祭犯,哼哼,不開心
項目中會用到將xml文件解析成json文件在頁面中顯示出來沃粗,以前jq的時候用到的方法行不通了,故在這邊介紹一種我覺得還不錯的插件
1. npm安裝
?npm i x2js
2.在main.js中引用
import x2jsfrom'x2js'//xml數(shù)據(jù)處理插件Vue.prototype.$x2js =newx2js()//創(chuàng)建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的錯