只需在安装时重新加载 vue 中的页面 [英] Reload page in vue just once in mounted

查看:28
本文介绍了只需在安装时重新加载 vue 中的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When user visit the page I want the page to refresh just once.

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

But if I place location.reload() in mounted() . It trigger infinity loop page reload

推荐答案

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

You just need to come up with a way of conditionally reloading the page to avoid the infinite reload.

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

One way is to set a value in local storage:

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

这篇关于只需在安装时重新加载 vue 中的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆