如何在 VueJs 的 axios `response` 中访问 `PromiseValue`
How to access `PromiseValue` in axios `response` in VueJs
我正在尝试在模式中显示 client
信息详细信息。点击后@click="showDetails(props.row.id)"
.
我在方法中使用 axios.get
并返回数据。现在,它将数据作为 PromiseValue 对象返回。如何访问 PromiseValue
以显示 HTML 中的值。有人可以帮我解决问题吗!我正在尝试如下 -
<button @click="showDetails(props.row.id)"><i class="fas fa-eye"></i></button>
并且在脚本中-
<script type="text/javascript">
import axios from 'axios';
export default {
data(){
return{
leftPanelVisiblity: false,
singleData: '', }
},
methods:{
showDetails(id){
let b = axios.get('/api/clients/'+id)
.then(function (response) {
return response.data;
}).catch(error => {
alert(error);
});
//console.log(b);
this.singleData = b;
this.leftPanelVisiblity = true
},
}
}
</script>
最后,我想访问或显示 leftPanelVisiblity
模态中的数据,例如 -
<p>Name: {{ this.singleData.name }}</p>
或
<p>Name: {{ this.singleData.email }}</p>
.
您不能在使用 Promises 时将 Axios 调用分配给变量(除非您正在使用 await/async)。
相反,您应该 运行在 then
回调中设置逻辑。否则对于 JavaScript 的同步性质,它会在请求完成之前 运行。您的代码应如下所示:
methods:{
showDetails(id){
axios.get('/api/clients/'+row.id).then(response => {
//Logic goes here
this.singleData = response.data
this.leftPanelVisibility = true
}).catch(error => {
alert(error);
});
}
}
您需要为您的 axios 的响应分配一个变量:
showDetails(id){
axios.get('/api/clients/'+id)
.then(function (response) {
this.singleData = response.data;
}).catch(error => {
alert(error);
});
console.log(this.sigleData);
this.leftPanelVisiblity = true
},
我正在尝试在模式中显示 client
信息详细信息。点击后@click="showDetails(props.row.id)"
.
我在方法中使用 axios.get
并返回数据。现在,它将数据作为 PromiseValue 对象返回。如何访问 PromiseValue
以显示 HTML 中的值。有人可以帮我解决问题吗!我正在尝试如下 -
<button @click="showDetails(props.row.id)"><i class="fas fa-eye"></i></button>
并且在脚本中-
<script type="text/javascript">
import axios from 'axios';
export default {
data(){
return{
leftPanelVisiblity: false,
singleData: '', }
},
methods:{
showDetails(id){
let b = axios.get('/api/clients/'+id)
.then(function (response) {
return response.data;
}).catch(error => {
alert(error);
});
//console.log(b);
this.singleData = b;
this.leftPanelVisiblity = true
},
}
}
</script>
最后,我想访问或显示 leftPanelVisiblity
模态中的数据,例如 -
<p>Name: {{ this.singleData.name }}</p>
或
<p>Name: {{ this.singleData.email }}</p>
.
您不能在使用 Promises 时将 Axios 调用分配给变量(除非您正在使用 await/async)。
相反,您应该 运行在 then
回调中设置逻辑。否则对于 JavaScript 的同步性质,它会在请求完成之前 运行。您的代码应如下所示:
methods:{
showDetails(id){
axios.get('/api/clients/'+row.id).then(response => {
//Logic goes here
this.singleData = response.data
this.leftPanelVisibility = true
}).catch(error => {
alert(error);
});
}
}
您需要为您的 axios 的响应分配一个变量:
showDetails(id){
axios.get('/api/clients/'+id)
.then(function (response) {
this.singleData = response.data;
}).catch(error => {
alert(error);
});
console.log(this.sigleData);
this.leftPanelVisiblity = true
},