简体   繁体   English

在 Angular 中的 localStorage.getItem 上未定义

[英]Undefined on localStorage.getItem in Angular

I'm working on a project with multiple components that need to share data saved in local storage.我正在开发一个包含多个组件的项目,这些组件需要共享保存在本地存储中的数据。 I manage to save them as an object with setItem but when I'm trying to get them I get "undefined" in console.log.我设法将它们保存为带有 setItem 的 object 但是当我尝试获取它们时,我在 console.log 中得到“未定义”。

Function for saving on (click): Function 用于保存(点击):

  saving() {
    let dataStorage = {
      departureDate: this.departureDate,
      returnDate: this.returnDate,
      departureAirport: this.departureAirport,
      arrivalAirport: this.destinationAirport,
      passengersNumber: this.numberOfPassengers
    };
    localStorage.setItem("flightdetails", JSON.stringify(dataStorage));
    this.showStorage = JSON.parse(localStorage.getItem("flightdetails"));
  }

  testLocal() {
    console.log(this.showStorage); //Here I get undefined
  }

All variables are declared:声明所有变量:

public numberOfPassengers: number = 1;
  public departureDate: any;
  public returnDate: any;
  public departureAirport: string;
  public destinationAirport: string;
  public showStorage: any;

Also I'm not sure how to call local storage element in another component to display.另外我不确定如何调用另一个组件中的本地存储元素来显示。 Currently I have component called summary:目前我有一个名为摘要的组件:

COMPONENT.TS:组件.TS:

export class SummaryComponent implements OnInit {
  public showStorage = "";
  constructor() {
    this.showStorage = localStorage.getItem("flightDetails");
  }

HTML for summary: HTML 总结:

Departure date: {{ showStorage.departureDate}}   

STACKBLITZ: https://stackblitz.com/edit/flight-date-pikcer STACKBLITZ: https://stackblitz.com/edit/flight-date-pikcer

Thanks for your support in solving this!感谢您对解决此问题的支持!

It's because the showStorage field is redeclared on each component init.这是因为showStorage字段在每个组件 init 上都重新声明了。 So after clicking To summary button which redirects to another view, the showStorage field is destroyed as well as the whole component.因此,在单击重定向到另一个视图的To summary按钮后, showStorage字段以及整个组件都会被销毁。 After going back to the FlightComponent , it's initialized again and that's why the showStorage is undefined .回到FlightComponent ,它再次被初始化,这就是为什么showStorageundefined的原因。 To make it work as you expect, you can assign it a value when creating FlightComponent :为了使其按预期工作,您可以在创建FlightComponent时为其分配一个值:

constructor(private router: Router) {
  this.showStorage = localStorage.getItem("flightdetails") || {};
}

That way the variable will hold the value of your localStorage item or an empty object if the value has not been set yet.这样,该变量将保存您的 localStorage 项目的值,或者如果尚未设置该值,则为空的 object。

Well, I do get value when I try this on my console.好吧,当我在控制台上尝试这个时,我确实得到了价值。 https://jsfiddle.net/wickedrahul/mp68hd09/2/ https://jsfiddle.net/wickedrahul/mp68hd09/2/

Chances are you might need to call saving() inside testLocal() so the value of this.showStorage could be assigned.您可能需要在testLocal() saving()可以分配this.showStorage的值。

  function saving() {
    let dataStorage = {
      departureDate: "foo",
      returnDate: "foo",
      departureAirport: "foo",
      arrivalAirport: "foo",
      passengersNumber: "foo"
    };
    localStorage.setItem("flightdetails", JSON.stringify(dataStorage));
    return JSON.parse(localStorage.getItem("flightdetails"));
  }

  function testLocal() {
    return saving();
  }

   var a = testLocal()
   console.log(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM