如何检索存储在localStorage中的对象的变量?

时间:2019-03-25 12:53:49

标签: angular typescript ionic-framework

例如,如何恢复存储在us_firstname中的对象的LocalStorage

 studentInfos() {
    this.eleveService.apiGetEleveInfo(this.token, this.us_id)
      .subscribe((data: any) => {

        this.student = data.results[0]

        localStorage.setItem("student", JSON.stringify(data));

        console.log('student', this.student)

      })
  }

the strucutre of the object

2 个答案:

答案 0 :(得分:1)

const student = JSON.parse(localstorage.getItem(“ student”))[0]

答案 1 :(得分:0)

您可以使用以下方法获取本地存储值:

getItem()方法返回指定存储对象项的值。

let student = localStorage.getItem("student");

// convert this to object
student = JSON.parse(student);