VueJS,无法访问同一方法中的"this"或"self"



目前,我一直在一个问题上呆了几个小时。可悲的是,我没有通过在世界各地搜索搜索来找到解决此问题的工作解决方案。

问题是,从API中提取数据后,我无法访问同一功能中的"自我"或"此"变量。我只是得到一个"未定义"。

getInvoice(){
  var self = this
  axios
    .get(url)
    .then(response => (self.invoice = response.data.data))
  console.log(self.invoice.invoice_nr) //undefined
},

最好的问候,预先感谢。

首先,这不是VUE JS问题。

REST API在JavaScript中是异步的,无论您使用哪种框架。

现在,要访问Promise的数据,您需要在.then函数中添加它。

看看Axios Docs

承诺

获取您的数据,

getInvoice(){
  var self = this
  axios
    .get(url)
    .then(response => {
        self.invoice = response.data.data;
        console.log(self.invoice.invoice_nr);
    })
},

希望这会有所帮助!

最新更新