如何使用window.location.reload重新加载一次

How can I reload once using window.location.reload?

本文关键字:加载 一次 新加载 window 何使用 location reload      更新时间:2023-09-26

我的web应用程序使用AngularJs,登录时遇到问题,我找到的唯一解决方案是登录后重新加载页面,但我只需要一次,但现在是一个无限循环。如何仅使用一次Window.location.reload()

登录方式:

.success(function (data) {
        $scope.tokengenerated = data.token;
        $cookies.put('Username', UsernameValue);
        $cookies.put('Token', $scope.tokengenerated);
        $location.path("/incidents");
})

第一次加载页面时,这样做:

wwww.example.com/incidents?reload=true

然后检查参数是否存在:

if (location.search.indexOf("reload=true") != -1) {
    // refresh the page, but no "reload" this time
    location.href = "www.example.com/incidents";
}

或者使用Angular.js:

if ($location.search().reload === "true") {
    // refresh the page, but no "reload" this time
    $location.path("/incidents");
}

或者,您可以在重新加载页面之前检查设置的cookie是否存在。如果存在cookie,请不要重新加载;如果不是,设置它,然后重新加载页面:

if (!$cookies.get("Token")) {
    // set cookies, do whatever you need here
    
    // reload it only once
    $location.path("/incidents");
}

试试这个。。。这是工作!

var refresh = $window.localStorage.getItem('refresh');
console.log(refresh);
if (refresh===null){
    window.location.reload();
    $window.localStorage.setItem('refresh', "1");
}