Joe*_*oey 2 javascript vue.js vuejs2
当用户访问该页面时,我希望页面只刷新一次.
但是,如果我把location.reload()在mounted().它会触发无限循环页面重新加载
Dec*_*oon 12
您只需要想出一种有条件地重新加载页面的方法,以避免无限重新加载.
一种方法是在本地存储中设置一个值:
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();
}
}
Run Code Online (Sandbox Code Playgroud)