如何在axios外部设置变量

编程入门 行业动态 更新时间:2024-10-28 00:20:57
本文介绍了如何在axios外部设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法使用此函数返回值,因为它为空.

I can't return a value using this function because it is empty.

getNameById (id) { var name = '' axios.get('/names/?ids=' + id) .then(response => { this.response = response.data name = this.response[0].name }) .catch(e => { this.errors.push(e) }) // Is empty console.log('Name ' + name) return name }

如何访问"then"中的名称变量并返回它?

How do I access the name variable inside "then" and return it?

推荐答案

您应该返回 promise .

getNameById (id) { return axios.get('/names/?ids=' + id) .then(response => { this.response = response.data return this.response[0].name }) }

并使用它:

getNameById(someId) .then(data => { // here you can access the data });

更多推荐

如何在axios外部设置变量

本文发布于:2023-10-09 16:57:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何在   axios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!