Hello I have this problem:in main.js I have a global function getSites
, needed to retrive a list of sites from API.
This function work with async/await and returns data sites asynchronously.
I also have a global variable called lockSitesLoading
which i set to true when i start to fetch data from API, I need this to prevent client to make a new request to server.
In the first function getSites
I need to check this variable lockSitesLoading
and, if it's true, start to watch it till it becomes false. At this point I want to call getSites
recursively.
The problem starts now, because when i start watch the variable this function doesn't wait for the variable to become false, but returns undefined
.
Here there is my code:
Compartant.vue:(用谷歌翻译翻译)
async mounted() {
let vm = this;
const sites = await vm.getSites();
vm.sites = sites.data
},
Main.js:
async getSites() {
let vm = this;
if (vm.$store.state.lockSitesLoading) {
vm.$watch('$store.state.lockSitesLoading', () => vm.getSites());
} else {
return vm._getSitesOnline();
//this is the function that effectively get datas from server
}
我试着用这种方法解决它,但行不通.有什么好主意吗?