简体   繁体   English

HTML5本地存储Angular 2

[英]HTML5 local storage Angular 2

I try to implement now a local Storage in Angular2 for the first time and get a bit confused. 我尝试首次在Angular2中实现一个本地存储,并且有点困惑。

So, I have First Component, where I register my user 所以,我有第一个组件,我注册我的用户

export class FormComponent {
  modus = ['first', 'second'];
  model: User = new User('', '', '');
  constructor(private _cookieService: CookieService) {}
}

Here is class, that I use in FormComponent 这是我在FormComponent使用的FormComponent

export class User {
  constructor (
    public email: string,
    public name: string,
    public modus: string
  ) {}
}

I bind it and everything in form works very good. 我绑定它,形式上的一切都很好。 Now I want to store it in local storage (3 parameters of User ) 现在我想将它存储在本地存储中( User 3个参数)

But how could I do this? 但我怎么能这样做?

I leave this page with registration of user and go to other pages, like 我离开这个页面注册用户并转到其他页面,如

export class Part1Component {
  public email;
  public name;
  public modus;

  constructor(private _location: Location) {}
     myTestFunction(){

    /* assign values here
       this.email = ;
       this.name = ;
       this.modus = ;
   */
       }


 }

How could I get values from storage and assign them here? 我如何从存储中获取值并在此处分配它们?

If you storing a user object in session storage by using JSON.stringify() then you can do this 如果使用JSON.stringify()在会话存储中存储用户对象,则可以执行此操作

let user = JSON.parse(localStorage.getItem('user'));
this.email=user.email;
this.name=user.name;

Typescript provides localStorage which you can use to set and get storage data. Typescript提供了localStorage ,您可以使用它来设置和获取存储数据。

Use the localStorage.setItem(key, value); 使用localStorage.setItem(key, value); method where you want to set the data. 要设置数据的方法。

localStorage.setItem('__user__email', user.email);
localStorage.setItem('__user__name', user.name);
localStorage.setItem('__user__modus', user.modus);

.. and localStorage.getItem(key); ..和localStorage.getItem(key); where you want to get the data. 你想要获取数据的地方。

myTestFunction() {

    // assign values here
    this.email = localStorage.getItem('__user__email');
    this.name  = localStorage.getItem('__user__name');
    this.modus = localStorage.getItem('__user__modus');
}

You can have a look at this utility class and use that I created in one of my projects: 您可以查看此实用程序类并使用我在其中一个项目中创建的实用程序类:

LOCAL STORAGE UTILITY CLASS 本地存储实用程序类

You should use localStorage.setItem() to save your data and localStorage.getItem() to get your saved data from localStorage. 您应该使用localStorage.setItem()来保存数据,使用localStorage.getItem()来从localStorage获取您保存的数据。 Please read this localStorage Documentation for more details. 有关更多详细信息,请阅读本地存储文档

Here is a simple example for using localStorage. 以下是使用localStorage的简单示例。

Save data to localStorage: 将数据保存到localStorage:

localStorage.setItem('userKey', JSON.stringify(this.model));

Beware that you can save only strings to localStorage. 请注意,您只能将字符串保存到localStorage。

Get saved data from localStorage for your Example: 从localStorage获取保存的数据作为示例:

let savedUser: User = JSON.parse(localStorage.getItem('userKey'));

If you want to clear or remove saved data you can use following code: 如果要清除或删除已保存的数据,可以使用以下代码:

localStorage.removeItem('userKey');

or 要么

localStorage.clear();

You can request your data from every point to localStorage in your application. 您可以在应用程序中将每个点的数据请求到localStorage。

use html5 localStorage like this 像这样使用html5 localStorage

localStorage.setItem("key",value) set a item and then you can get it even you close the borwser. localStorage.setItem(“key”,value)设置一个项目,然后即使你关闭了borwser你也可以得到它。

localStorage.getItem("key"); localStorage.getItem( “钥匙”);

and it save as string 并保存为字符串

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

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