Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

javascript - Loss of Information Between Pages

I have a page where I look for certain information.
When I click on the edit button, I charge this information in a localStorage variable, and redirect the user to another page.
In the onInit method of this new page, I charge the information in the respective variables, that's how far it works perfectly.
Now, every time that I leave this new page and come back, the localStorage information is being loaded, which shouldn't happen, so before the localStorage information is being loaded,
I need to detect whether the user is coming with the intention of editing the page or just browsing.
To try to solve it, I create a simple variable that works like Get and Set, when the user clicks on edit, before redirect, I change this variable ("this.isEdit")
to "true" and in the OnInit of the redirected page, I want to check what value the variable has, to know If I have to preload the information saved in localStorage or not.
The problem is that the state of "this.isEdit" is not being maintained.
Does anyone know what I'm doing wrong or have any other suggestions?>
I'd really appreciate it.

This is my service file (common file for twice pages):

 public isEdit: any;

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


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

getEditMood() {
  return this.isEdit;
}

Here I am seting this variable, and made a query to preload information in localStorage.In this point this.mappingService.getEditMood() is true when I made console.log

editFileRequest(fileN: any) {
    var businessObj = this.searchFile.value["bussinessObject"];
    // 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["bussinessObject"]));
      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 is the page on that I need to get information according to the status this.mappingService.getEditMood() that, at this time should be true.

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.bussinessObject().subscribe((bussiness: any) => {
            //debugger;
            this.listBussinessObj = bussiness;
        })

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...