Angular 5 ngx-permissions正在删除页面刷新的所有权限....如何避免删除权限?

时间:2018-03-17 05:48:32

标签: angular

permissions after login

permissions after refresh deleted

页面刷新权限为空后

刷新后如何避免删除权限

2 个答案:

答案 0 :(得分:0)

如果您希望数据在会话中保持不变,则可以使用浏览器的LocalStorage API。

添加项目

this.permissions = JSON.parse(window.localStorage.getItem("permissions"));

要检索它,

window.localStorage.removeItem("permissions");

要删除项目,

song

您可以在Chrome开发者工具中检查localStorage项目,在应用程序>存储>本地存储

注意:如果未启用Cookie,此方法将失败。

PS:我不建议在LocalStorage中存储权限,最好从API调用中获取它们。

答案 1 :(得分:0)

在AppModule中添加请求以在每次刷新后检索权限

import { Component, OnInit } from '@angular/core';
import { NgxPermissionsService } from 'ngx-permissions';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  title = 'app';

   constructor(private permissionsService: NgxPermissionsService,
               private http: HttpClient) {}

  ngOnInit(): void {
    const perm = ["ADMIN", "EDITOR"];

    this.permissionsService.loadPermissions(perm);

     this.http.get('url').subscribe((permissions) => {
       //const perm = ["ADMIN", "EDITOR"]; example of permissions
       this.permissionsService.loadPermissions(permissions);
    })
  }
}

更多信息:https://github.com/AlexKhymenko/ngx-permissions/wiki