javascript 在 AngularJs 中注销时清除服务中数据的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26522875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:05:01  来源:igfitidea点击:

Best practices for Clearing data in services on logout in AngularJs

javascriptangularjs

提问by CodeTower

I have several services which uses a webservice and caches a lot of the results.By caching i mean storing in a variable on the service. When a user logs out the data should be cleared. The services looks like the following (simplified version):

我有几个服务使用 web 服务并缓存了很多结果。缓存我的意思是存储在服务的变量中。当用户注销时,应清除数据。服务如下所示(简化版):

class DataService   {

  private data;

  constructor($http)
  {
     $http.get(url).then((response) => 
       { 
          this.data = response.data;
       });
  }

}

}

Which is typescript, but resolves to something like this:

这是打字稿,但解析为这样的:

var DataService = (function () {
    function DataService($http) {
        var _this = this;
        $http.get(url).then(function (response) {
            _this.data = response.data;
        });
    }
    return DataService;
})();

I could clear the data using the answer in This QuestionWhich is doing something like this:

我可以使用This Question 中的答案清除数据,该问题正在执行以下操作:

$rootScope.on('logout',function(){
  this.data = [];
});

However, this is a lot of code when we have multiple services and controllers. And we all know that this new guy will add some new data to a service and he forgets to add it to the logout sequence. It is simply a bad practice.

然而,当我们有多个服务和控制器时,这是很多代码。我们都知道这个新人会向服务添加一些新数据,但他忘记将其添加到注销序列中。这只是一种不好的做法。

Similarily, data are stored on $scope in various parts of the application and this must be cleared as well. The scope is rather simple as constructors for the controllers are loaded on each page visits and will then override the data.

类似地,数据存储在应用程序的各个部分的 $scope 中,这也必须清除。范围相当简单,因为控制器的构造函数在每次页面访问时加载,然后将覆盖数据。

One propossed solution is to make a refresh, however this gives horrible user experiences.

一个提议的解决方案是进行刷新,但这会带来糟糕的用户体验。

One solution could be to make angular believe that the services have never been created or reload angular completely.

一种解决方案可能是让 angular 相信服务从未被创建或完全重新加载 angular。

What is the best way to do this? Is it bad to store data in variables on services?

做这个的最好方式是什么?将数据存储在服务的变量中是不是很糟糕?

采纳答案by Torsten Engelbrecht

You can create a service which is responsible clearing all the the data from the other services. In this case you only have to call the "clear" method of this service, and implement the individual clear calls inside of this one (so only implemented once).

您可以创建一个服务,负责清除其他服务中的所有数据。在这种情况下,您只需调用clear此服务的“

This gives you also the advantage that you have an overview of which services data need to be cleared all the time, since you might have exceptions in the future.

这还为您提供了一个优势,即您可以随时了解哪些服务数据需要清除,因为您将来可能会遇到例外情况。

Also see this answer for your reference: Injecting a service into another service in angularJS

另请参阅此答案以供参考:Injecting a service into another service in angularJS

回答by Marian Ban

you can use a full page refresh:

您可以使用整页刷新:

$window.location.reload();

this will restart the whole state of your application

这将重新启动应用程序的整个状态

another solution could be to store everything in session storage and then clear it on log out:

另一种解决方案可能是将所有内容存储在会话存储中,然后在注销时将其清除:

sessionStorage.clear();