在mounted中重新加载vue中的页面一次

问题描述 投票:0回答:2

当用户访问该页面时,我希望页面仅刷新一次。

但是如果我将

location.reload()
放入
mounted()
中。它会触发无限循环页面重新加载

javascript vue.js vuejs2
2个回答
27
投票

您只需要想出一种有条件地重新加载页面的方法来避免无限重新加载。

一种方法是在本地存储中设置一个值:

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();
    }
}

0
投票

我复制相同的逻辑并使用 onMounted 实现,它再次渲染多次问题未解决

© www.soinside.com 2019 - 2024. All rights reserved.