admin管理员组文章数量:1031308
I'm having this issue where I'm using Vue3 and Nuxt, and I need a loading function to run once everytime the user navigates to the page, but delay loading if the global data isn't available yet.
const depositsLoading = ref(true);
const fetchDeposits = async () => {
//... Load locally dependent data
};
const stopCompaniesWatch = watch(
() => companies.selected, // check to see if the global data that loads on app load is available
(loaded) => {
console.log('WATCHER RUNNING: ', loaded)
if (loaded || companies.selected) {
console.log('LOADED TRUE')
fetchDeposits();
stopCompaniesWatch();
}
}
)
This code works as expected on page refresh, but not on page navigate away and navigate back (nav back). On nav back, the depositsLoading is set to true and the page is stuck in the loading state forever. Here's what I've learned happens on nav back:
depositsLoading
is true again telling medepositsLoading=ref(true)
was ran again.- No
console.log
statements run, telling me that thewatch
did not run again, even though the watch callback is supposed to be run once on page load. onMounted
does not run.
So its like some of the setup tag reruns on nav back but not all of it?
My quesitons:
- Why is this happening?
- How do I make my page refecth locally dependent data on nav back?
MRE - no idea how to provide one as this question depends on navigation, so at the least it requires a whole nuxt server. If someone knows how I could provide this let me know.
I'm having this issue where I'm using Vue3 and Nuxt, and I need a loading function to run once everytime the user navigates to the page, but delay loading if the global data isn't available yet.
const depositsLoading = ref(true);
const fetchDeposits = async () => {
//... Load locally dependent data
};
const stopCompaniesWatch = watch(
() => companies.selected, // check to see if the global data that loads on app load is available
(loaded) => {
console.log('WATCHER RUNNING: ', loaded)
if (loaded || companies.selected) {
console.log('LOADED TRUE')
fetchDeposits();
stopCompaniesWatch();
}
}
)
This code works as expected on page refresh, but not on page navigate away and navigate back (nav back). On nav back, the depositsLoading is set to true and the page is stuck in the loading state forever. Here's what I've learned happens on nav back:
depositsLoading
is true again telling medepositsLoading=ref(true)
was ran again.- No
console.log
statements run, telling me that thewatch
did not run again, even though the watch callback is supposed to be run once on page load. onMounted
does not run.
So its like some of the setup tag reruns on nav back but not all of it?
My quesitons:
- Why is this happening?
- How do I make my page refecth locally dependent data on nav back?
MRE - no idea how to provide one as this question depends on navigation, so at the least it requires a whole nuxt server. If someone knows how I could provide this let me know.
Share Improve this question edited Mar 4 at 22:53 Jamie Marshall asked Mar 4 at 22:48 Jamie MarshallJamie Marshall 2,3265 gold badges38 silver badges65 bronze badges1 Answer
Reset to default 1setup tag reruns
Of course setup
will be reruned when you navigate back if this component is not keep alived
the watch callback is supposed to be run once on page load
The watch callback is not supposed to be run once on page load. It runs when you refresh the page because companies.selected
changes from false
to true
. But when you navigate back, companies.selected
is always true
and won't trigger the watch callback. If you still want the callback to run, pass the immediate: true
option. Ref: https://vuejs./guide/essentials/watchers.html#eager-watchers
Bonus
I would recommend https://vueuse./shared/until/#until if you are using Vueuse. Example:
;(async () => {
await until(() => companies.selected).toBe(true)
fetchDeposits();
})()
I'm having this issue where I'm using Vue3 and Nuxt, and I need a loading function to run once everytime the user navigates to the page, but delay loading if the global data isn't available yet.
const depositsLoading = ref(true);
const fetchDeposits = async () => {
//... Load locally dependent data
};
const stopCompaniesWatch = watch(
() => companies.selected, // check to see if the global data that loads on app load is available
(loaded) => {
console.log('WATCHER RUNNING: ', loaded)
if (loaded || companies.selected) {
console.log('LOADED TRUE')
fetchDeposits();
stopCompaniesWatch();
}
}
)
This code works as expected on page refresh, but not on page navigate away and navigate back (nav back). On nav back, the depositsLoading is set to true and the page is stuck in the loading state forever. Here's what I've learned happens on nav back:
depositsLoading
is true again telling medepositsLoading=ref(true)
was ran again.- No
console.log
statements run, telling me that thewatch
did not run again, even though the watch callback is supposed to be run once on page load. onMounted
does not run.
So its like some of the setup tag reruns on nav back but not all of it?
My quesitons:
- Why is this happening?
- How do I make my page refecth locally dependent data on nav back?
MRE - no idea how to provide one as this question depends on navigation, so at the least it requires a whole nuxt server. If someone knows how I could provide this let me know.
I'm having this issue where I'm using Vue3 and Nuxt, and I need a loading function to run once everytime the user navigates to the page, but delay loading if the global data isn't available yet.
const depositsLoading = ref(true);
const fetchDeposits = async () => {
//... Load locally dependent data
};
const stopCompaniesWatch = watch(
() => companies.selected, // check to see if the global data that loads on app load is available
(loaded) => {
console.log('WATCHER RUNNING: ', loaded)
if (loaded || companies.selected) {
console.log('LOADED TRUE')
fetchDeposits();
stopCompaniesWatch();
}
}
)
This code works as expected on page refresh, but not on page navigate away and navigate back (nav back). On nav back, the depositsLoading is set to true and the page is stuck in the loading state forever. Here's what I've learned happens on nav back:
depositsLoading
is true again telling medepositsLoading=ref(true)
was ran again.- No
console.log
statements run, telling me that thewatch
did not run again, even though the watch callback is supposed to be run once on page load. onMounted
does not run.
So its like some of the setup tag reruns on nav back but not all of it?
My quesitons:
- Why is this happening?
- How do I make my page refecth locally dependent data on nav back?
MRE - no idea how to provide one as this question depends on navigation, so at the least it requires a whole nuxt server. If someone knows how I could provide this let me know.
Share Improve this question edited Mar 4 at 22:53 Jamie Marshall asked Mar 4 at 22:48 Jamie MarshallJamie Marshall 2,3265 gold badges38 silver badges65 bronze badges1 Answer
Reset to default 1setup tag reruns
Of course setup
will be reruned when you navigate back if this component is not keep alived
the watch callback is supposed to be run once on page load
The watch callback is not supposed to be run once on page load. It runs when you refresh the page because companies.selected
changes from false
to true
. But when you navigate back, companies.selected
is always true
and won't trigger the watch callback. If you still want the callback to run, pass the immediate: true
option. Ref: https://vuejs./guide/essentials/watchers.html#eager-watchers
Bonus
I would recommend https://vueuse./shared/until/#until if you are using Vueuse. Example:
;(async () => {
await until(() => companies.selected).toBe(true)
fetchDeposits();
})()
本文标签: vuejsCachingwatchrefsand navigation behviorStack Overflow
版权声明:本文标题:vue.js - Caching - watch, refs, and navigation behvior - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745025610a2130292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论