![](/img/trans.png)
[英]Get value from promise react native and cominbing to Axios header
[英]VueJS get value from an axios promise
我有這個代碼
<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>
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;
},
問題是console.log
顯示了預期的值但是在Vue渲染上我得到了這個[object Promise]
我想知道如何在promise解析后顯示值。
謝謝你的幫助
您的代碼是返回axios,它是一個promise對象。 如果你想返回最后的持續時間。 您應該等待axios承諾並返回響應。
async getDuration(id) {
var duration = '';
const response = await axios.post('/api/v1/checkvideo', {
menu: id
})
return response.data[0].datas.duration;
}
由於async
返回一個promise,[object Promise]將始終在屏幕上呈現。 如果你想獲得特定id的持續時間,我認為這是正確的。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.