Parse XML from Axios response, pushing to Vue data array

前端 未结 2 1693
不知归路
不知归路 2021-02-04 12:16

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

相关标签:
2条回答
  • 2021-02-04 12:42

    Answer above is correct. However, I would just use arrow functions everywhere so the this is always the VUE class component. Also, I would check for parsing errors.

    axios.get('http://url.to/events.xml')
      .then(response => {
        parseString(response.data, (err, result) => {
          if(err) {
           //Do something
          } else {
           this.events = result
         }
        });        
      })
    }
    
    0 讨论(0)
  • 2021-02-04 12:47

    Try not to use this inside parseString, maybe it's a scope issue, which means this isn't referring to the vue data object.

    Try this:

    axios.get('http://url.to/events.xml')
      .then(response => {
        var self = this; 
        parseString(response.data, function (err, result) {
          self.events = result
        });        
      })
    }
    
    0 讨论(0)
提交回复
热议问题