当用户访问该页面时,我希望页面仅刷新一次。
但是如果我将
location.reload()
放入 mounted()
中。它会触发无限循环页面重新加载
您只需要想出一种有条件地重新加载页面的方法来避免无限重新加载。
一种方法是在本地存储中设置一个值:
mounted() {
if (localStorage.getItem('reloaded')) {
// The page was just reloaded. Clear the value from local storage
// so that it will reload the next time this page is visited.
localStorage.removeItem('reloaded');
} else {
// Set a flag so that we know not to reload the page twice.
localStorage.setItem('reloaded', '1');
location.reload();
}
}
我复制相同的逻辑并使用 onMounted 实现,它再次渲染多次问题未解决