Php 在vuejs中从ajax返回值
我试图将一个整数(id)传递给一个调用api的函数。然后,api检查传递的id是否与数据库中的任何数据匹配,并返回与该id关联的名称。下面是我的代码Php 在vuejs中从ajax返回值,php,ajax,laravel,vue.js,Php,Ajax,Laravel,Vue.js,我试图将一个整数(id)传递给一个调用api的函数。然后,api检查传递的id是否与数据库中的任何数据匹配,并返回与该id关联的名称。下面是我的代码 <tr v-for="store in storeList" :key="store.id"> <td>{{ getNodeName(store.store_name) }}</td> </tr> getNodeName(nodeId)
<tr v-for="store in storeList" :key="store.id">
<td>{{ getNodeName(store.store_name) }}</td>
</tr>
getNodeName(nodeId)
{
axios.get('api/store/getNodeName/'+nodeId).then(function (response){
return response.data[0].name;
});
}
{{getNodeName(store.store_name)}
getNodeName(节点ID)
{
get('api/store/getNodeName/'+nodeId)。然后(函数(响应){
返回响应。数据[0]。名称;
});
}
现在的问题是如何在td标签内打印结果。显然,从ajax返回时不起作用,我尝试将其全部推到一个数组中并再次打印,但也不起作用。
谢谢假设您的API可以工作,那么您做错的第一件事就是在承诺得到解决时从回调返回,而不是从
getNodeName
方法返回
实现所需的一种简单方法是在挂载的生命周期挂钩(此处使用)中循环浏览存储列表(假设它是道具)
。。。
{{node.name}
...
数据(){
返回{
节点:[]
};
},
安装的(){
this.storeList.forEach(store=>this.getNodeName(store.store_name));
},
方法:{
getNodeName(节点ID){
get('api/store/getNodeName/'+nodeId)
.then(response=>this.nodes.push({id:nodeId,name:response.data[0].name}))
}
}
...
如果可能的话,您可能还想将其转换为一个API调用,因为您正在进行storeList.length
调用。您可以对storeList进行循环,从中获取nodeId,然后进行API调用
<tr v-for="store in storeData" :key="store.id">
<td>{{store.name}} </td>
</tr>
data(){
return{
storeData : []
};
{{store.name}
数据(){
返回{
storeData:[]
};
},
created(){
对于(var i=0;i this.storeData.push({id:this.storeList[i].store\u name,
名称:响应。数据[0]。名称
}))
}
}this.storeList.push(response.data[0]);
created(){
for(var i=0; i<this.storeList.length; i++){
axios.get('api/store/getNodeName/' + this.storeList[i].store_name)
.then(response => this.storeData.push({ id: this.storeList[i].store_name,
name: response.data[0].name
}))
}