Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload page in vue just once in mounted

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

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

like image 990
Joey Avatar asked May 07 '18 12:05

Joey


1 Answers

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();
    }
}
like image 106
Decade Moon Avatar answered Nov 05 '22 08:11

Decade Moon