Javascript 如何检查数据是否从api返回?
如何检查我的api请求是否从Javascript 如何检查数据是否从api返回?,javascript,css,reactjs,typescript,vue.js,Javascript,Css,Reactjs,Typescript,Vue.js,如何检查我的api请求是否从fetchData函数返回任何数据?我想将boolean(或其他内容)返回到我的Index.vue,并在加载数据时显示加载程序,但在加载数据时,我想使用:this.$router.push(“管理/仪表板”)要重定向到显示此数据的另一页 我的代码: const actions = { fetchData({ commit },{ id}) { return axios.get(`someUrl?hd=${id}`) .t
fetchData
函数返回任何数据?我想将boolean
(或其他内容)返回到我的Index.vue
,并在加载数据时显示加载程序,但在加载数据时,我想使用:this.$router.push(“管理/仪表板”)
要重定向到显示此数据的另一页
我的代码:
const actions = {
fetchData({ commit },{ id}) {
return axios.get(`someUrl?hd=${id}`)
.then(response => {
if(response.status == 200) {
commit('setData', response.data),
}
})
.catch(
error => {
console.log(error);
}
);
}
}
Index.vue
<div v-if="isDataLoaded" class="loading-div">
<vue-spinner />
</div>
methods: {
...mapActions(["fetchData"]),
launchFetchData() {
new Promise(() => {
this.$store.dispatch("fetchData", {
id: this.id
})
})
}
方法:{
…映射操作([“fetchData”]),
launchFetchData(){
新承诺(()=>{
此.$store.dispatch(“fetchData”{
id:这个是
})
})
}
感谢您提供的帮助:
console.log(response.data)
当您将数据设置为response.data时,请使用.length检查大小。如果大于0,则将用户路由到仪表板。以这种方式使用Js Promission。下一个all语句将暂停,直到promise解决问题
函数makeGetRequest(路径){
返回新承诺(函数(解析、拒绝){
获取(路径)。然后(
(答复)=>{
var结果=响应状态;
log(“处理请求”);
决心(结果);
},
(错误)=>{
拒绝(错误);
}
);
});
}
异步函数main(){
var result=await makeGetRequest('https://jsonplaceholder.typicode.com/users/1');
控制台日志(“状态”,结果);
console.log(“语句1”);
console.log(“语句2”);
}
main();
console.log('starting…');