admin管理员组文章数量:1315326
I have this code
Template :
<a href="#" class="video" @click.prevent="checkVideo(subItem.id)" :id="subItem.id" data-toggle="modal" data-target="#playerModal">{{subItem.name}}<span>{{getDuration(subItem.id)}}</span></a>
VueJS Function :
async getDuration(id) {
var duration = '';
await axios.post('/api/v1/checkvideo', {
menu: id
})
.then(function (response) {
console.log(response.data[0].datas.duration)
return response.data[0].datas.duration;
});
//console.log(duration);
//return duration;
},
The problem is that the console.log
show the values as expected but on the Vue Rendering I get this [object Promise]
I want to know how to show the values after the promise has been resolved.
Thanks for helping
I have this code
Template :
<a href="#" class="video" @click.prevent="checkVideo(subItem.id)" :id="subItem.id" data-toggle="modal" data-target="#playerModal">{{subItem.name}}<span>{{getDuration(subItem.id)}}</span></a>
VueJS Function :
async getDuration(id) {
var duration = '';
await axios.post('/api/v1/checkvideo', {
menu: id
})
.then(function (response) {
console.log(response.data[0].datas.duration)
return response.data[0].datas.duration;
});
//console.log(duration);
//return duration;
},
The problem is that the console.log
show the values as expected but on the Vue Rendering I get this [object Promise]
I want to know how to show the values after the promise has been resolved.
Thanks for helping
Share Improve this question asked May 6, 2019 at 9:22 Rahmani Seif el mouloukRahmani Seif el moulouk 1112 gold badges3 silver badges10 bronze badges1 Answer
Reset to default 6Your code is return axios which is a promise object. If you want to return final duration. You should await axios promise and return the response.
async getDuration(id) {
var duration = '';
const response = await axios.post('/api/v1/checkvideo', {
menu: id
})
return response.data[0].datas.duration;
}
EDITED
Since async
return a promise, [object Promise] will always be rendered on the screen. If you want to get duration on a specific id, I think this is correct to do.
data() {
return {
id: null, // Maybe you should store the value of `id` here
duration: null
}
},
mounted() {
this.getDuration(this.id)
},
methods: {
async getDuration(id) {
var duration = '';
const response = await axios.post('/api/v1/checkvideo', {
menu: id
})
this.duration = response.data[0].datas.duration;
}
}
本文标签: javascriptVueJS get value from an axios promiseStack Overflow
版权声明:本文标题:javascript - VueJS get value from an axios promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977317a2408200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论