【问题标题】:Reload page in vue just once in mounted仅在已安装的情况下在 vue 中重新加载页面
【发布时间】:2018-05-07 12:09:48
【问题描述】:

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

但如果我将 location.reload() 放在 mounted() 中。它触发无限循环页面重新加载

【问题讨论】:

  • 生命周期钩子会在页面重新加载时重新启动,因此您需要以某种方式将其存储在某个地方
  • 分享你的代码和你重新加载和停止重新加载的条件?

标签: javascript vue.js vuejs2


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多