首页 > 解决方案 > 页面之间的信息丢失

问题描述

我有一个页面,我可以在其中查找某些信息。
当我点击编辑按钮时,我在 localStorage 变量中收取此信息,并将用户重定向到另一个页面。
在这个新页面的 onInit 方法中,我在各个变量中收取信息,这就是它完美运行的程度。
现在,每次我离开这个新页面并返回时,都会加载localStorage信息,这不应该发生,所以在加载localStorage信息之前,
我需要检测用户是否有编辑意图页面或只是浏览。
为了解决这个问题,我创建了一个像 Get 和 Set 一样工作的简单变量,当用户点击编辑时,在重定向之前,我更改了这个变量(“this.isEdit”)
为“true”并在重定向页面的 OnInit 中,我想检查变量的值,以了解是否必须预加载保存在 localStorage 中的信息。
问题是“this.isEdit”的状态没有得到维护。
有谁知道我做错了什么或有任何其他建议?>
我真的很感激。

这是我的服务文件(两页的通用文件):

 public isEdit: any;

  constructor(private http: HttpClient) { }
// Control edition of file


setEditMood(data: any) {
  this.isEdit = data;
}

getEditMood() {
  return this.isEdit;
}

这里我设置了这个变量,并查询了localStorage中的预加载信息。此时this.mappingService.getEditMood()在我制作console.log时为真

editFileRequest(fileN: any) {
    var businessObj = this.searchFile.value["businessObject"];
    // Call API to Load file information of Business Object + FileName combination
    this.configurationFileService.fileInformation(businessObj, fileN).subscribe((information: any) => {
      // Popullate the dataMapping table and form
      var mood = "true";
      var constt = this.mappingService.setEditMood(mood);
      localStorage.setItem("businessObjSelected", JSON.stringify(this.searchFile.value["businessObject"]));
      localStorage.setItem("fileNameSelected", JSON.stringify(fileN));
      localStorage.setItem("dataMappingCashedData", JSON.stringify(information));
      //localStorage.setItem("table", JSON.stringify(information));
      // Redirect to dataMapping table
      console.log('Edited', this.mappingService.getEditMood());
      this.router.navigate(['/app/datamapping']);
    });
  }

这是我需要根据状态 this.mappingService.getEditMood() 获取信息的页面,此时应该为真。

ngOnInit(): void {
      var isEditingr = this.mappingService.getEditMood();
      console.log('Get', isEditingr);
      if(isEditingr == "true"){
    this.sel_businessObject = JSON.parse(localStorage.getItem("businessObjSelected"));
     this.fileNameToEdit = JSON.parse(localStorage.getItem("fileNameSelected"));
     this.dataMappingData = JSON.parse(localStorage.getItem("dataMappingCashedData"));
      }

        // Call API to Load Business Object
        this.mappingService.businessObject().subscribe((business: any) => {
            //debugger;
            this.listBusinessObj = business;
        })

        this.mappingService.existingTables().subscribe((existingTables: any) => {
            this.listTable = existingTables;
        })
    }

标签: javascriptangular10

解决方案


推荐阅读