从Axios响应中解析XML,推送到Vue数据数组 [英] Parse XML from Axios response, pushing to Vue data array
本文介绍了从Axios响应中解析XML,推送到Vue数据数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Vue应用程序中,我使用Axios获取了一个XML文件,并使用parseString将XML解析为JSON.然后,我需要将result
传递给Vue数据(this.events).我的控制台日志将已解析的XML显示为Json,但无法在此函数中推送到Vue数据.
In my Vue app, I get a XML file with Axios and use parseString to parse XML as JSON. I then need to pass the result
to Vue data (this.events). My console log shows the parsed XML as Json, but I cannot push to Vue data inside this function.
var parseString = require('xml2js').parseString;
axios.get(`http://url.to/events.xml`)
.then(response => {
parseString(response.data, function (err, result) {
console.log(result); // returns a json array
this.events = result // nothing happens
});
})
}
如何在Vue中将JSON数组存储到this.data?
How to store my JSON array to this.data in Vue?
推荐答案
尽量不要在parseString
中使用this
,这可能是范围问题,这意味着this
并未引用vue数据对象.
Try not to use this
inside parseString
, maybe it's a scope issue, which means this
isn't referring to the vue data object.
尝试一下:
axios.get('http://url.to/events.xml')
.then(response => {
var self = this;
parseString(response.data, function (err, result) {
self.events = result
});
})
}
这篇关于从Axios响应中解析XML,推送到Vue数据数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文