最近从ng1  转ng2     相信 使用ng1的同学都知道 ngStorage 这个插件吧,  存储 本地数据

下面介绍一下 ng2 使用 localStorage

参考

github

https://github.com/marcj/angular2-localstorage

使用方法

1 nodejs 下载  npm install --save angular2-localstorage

2 引入到AppModule

import {Component} from "angular2/core";
import {WebStorageModule, LocalStorageService} from "angular2-localstorage";@NgModule({import: [WebStorageModule]
@Component({providers: [LocalStorageService]
})
export class AppModule {}

3 组件中使用

import {LocalStorage, SessionStorage} from "angular2-localstorage/WebStorage";class MySuperComponent {@LocalStorage() public lastSearchQuery:Object = {};@LocalStorage('differentLocalStorageKey') public lastSearchQuery:Object = {};
}

注意:在您正在使用的属性上始终定义默认值@LocalStorage

注意:localstorage键是默认的属性名称。定义@LocalStorage设置不同的第一个参数。

注意:请不要存储循环结构,因为此库在使用LocalStorage之前使用JSON.stringify进行编码。

注意:localstorage 后 再次刷新页面的时候,会先去 读取本地资源值,给属性赋值

举个栗子

@Component({selector: 'app-login',template: `
<form><div><input type="text" [(ngModel)]="username" placeholder="Username" /><input type="password" [(ngModel)]="password" placeholder="Password" /></div><input type="checkbox" [(ngModel)]="rememberMe" /> Keep me logged in<button type="submit">Login</button>
</form>`
})
class AppLoginComponent {//here happens the magic. `username` is always restored from the localstorage when you reload the site@LocalStorage() public username:string = '';public password:string;//here happens the magic. `rememberMe` is always restored from the localstorage when you reload the site@LocalStorage() public rememberMe:boolean = false;
}
@Component({selector: 'admin-menu',template: `
<div *ngFor="#menuItem of menuItems() | mapToIterable; #i = index"><h2 (click)="hiddenMenuItems[i] = !!!hiddenMenuItems[i]">{{i}}: {{category.label}}</h2><div style="padding-left: 15px;" [hidden]="hiddenMenuItems[i]"><a href>Some sub menu item 1</a><a href>Some sub menu item 2</a><a href>Some sub menu item 3</a></div>
</div>`
})
class AdminMenuComponent {public menuItems = [{title: 'Menu1'}, {title: 'Menu2'}, {title: 'Menu3'}];//here happens the magic. `hiddenMenuItems` is always restored from the localstorage when you reload the site@LocalStorage() public hiddenMenuItems:Array<boolean> = [];//here happens the magic. `profile` is always restored from the sessionStorage when you reload the site from the current tab/browser. This is perfect for more sensitive information that shouldn't stay once the user closes the browser.@SessionStorage() public profile:any = {};
}

  

转载于:https://www.cnblogs.com/richard1015/p/7269905.html

angular2 localStorage的使用相关推荐

  1. ASP.NET Core与Dapper和VS 2017使用JWT身份验证WEB API并在Angular2客户端应用程序中使用它

    目录 介绍 背景 步骤1 创建ASP.NET Core Web API项目 Fitness.JWT.API项目说明 使用代码 startup.cs JwtIssuerOptions.cs JwtCon ...

  2. Cocos Creator里localStorage的使用

    存储数据 cc.sys.localStorage.setItem('data', this._data); 存的时候会自动变成字符串,所以我们存二位数组的时候用JSON.stringify cc.sy ...

  3. php缓存数据到本地缓存,本地缓存localStorage的使用方法

    本篇文章的使用方法来自于实际开发,详细介绍AppCan手机框架本地缓存localStorage的使用方法. 在手机应用中,存储本地数据是必不可少的功能,例如我们可以在手机中存储用户自定义设置.传递数据 ...

  4. 封装 localStorage 缓存,兼容网页,微信小程序,uni-app

    封装的缓存功能,兼容网页,微信小程序,uni-app 使用,支持设置缓存,获取缓存,移除缓存,清空缓存,设置缓存时间,分组缓存设置. 把最下面的 Str4.js 代码拷贝到项目内可以直接使用,调用方式 ...

  5. JS 缓存 设置临时缓存和长期缓存 sessionStorage localStorage

    QQ技术交流群 173683866 526474645 欢迎加入交流讨论,打广告的一律飞机票 使用 Window sessionStorage 和 localStorage 属性 sessionSto ...

  6. HTML5跳转页面并传值以及localStorage的用法

    1.首先,你得在那个页面把数据存入localStorage中吧.这个是必须的! localStorage.setItem("user",JSON.stringify(data.al ...

  7. 浅谈 sessionStorage、localStorage、cookie 的区别以及使用

    1.sessionStorage.localStorage.cookie 之间的区别 相同点 cookie 和 webStorage 都是用来存储客户端的一些信息 不同点 localStorage l ...

  8. session,cookie,sessionStorage,localStorage的区别及应用场景

    浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...

  9. 在像Angular2这样的SPA应用中使用Google Analytics的方法

    Angular2のようなシングルページアプリケーションでGoogleアナリティクスを使う方法 如何在像Angular2这样的SPA应用中使用Google Analytics? 试着调查了一下. 由于S ...

  10. 理解 angular2 基础概念和结构 ----angular2系列(二)

    前言: angular2官方将框架按以下结构划分: Module Component Template Metadata Data Binding Directive Service Dependen ...

最新文章

  1. leetcode算法题--从上到下打印二叉树 II
  2. INotifyPropertyChanged 接口
  3. Java中Filter、Listener,拦截器的学习,listener、 filter、servlet 加载顺序及其详解
  4. 一个.NET Core开发者的Linux入门学习笔记
  5. ICanPay 统一支付网关
  6. I2C总线串行串行输入输出结构
  7. jQtouch 初体验
  8. [转载] 王长松:传统文化与中医养生(东南大学)——第2讲 中医养生方法的源泉(上)...
  9. Python判断字符串是否为数字(数字、小数、负数、负小数、0)
  10. Arduino 利用串口缓冲区监听的方式读取数据
  11. 【转载】手机快充的核心模块:ChargePump
  12. Win10 系统设置共享文件
  13. 人工神经网络与深度神经网络
  14. linux刷新分区表,linux磁盘分区的详细步骤(图解linux分区命令使用方法)
  15. 基于阿里云 Serverless 函数计算开发的疫情数据统计推送机器人
  16. 计算机专业对身体伤害大,电脑对人体的三大伤害
  17. 新时代交互英语上机答案(视听说)
  18. 惠普战66系列拆机图文详解
  19. vue-cropper
  20. Ubuntu 18.04下触控板右键失灵的解决方法

热门文章

  1. [渝粤教育] 西南科技大学 信息法律法规 在线考试复习资料2021版
  2. hervorgehen ( aus ... )
  3. QT All Modules QT所有模块
  4. Linux 中的 DTrace :BPF 进入 4.9 内核
  5. Hutool工具里,POST方法,body中传参的几种调用方法
  6. webstorm 高效开发 (html)
  7. VB / VS 多语言软件设计
  8. python练习题4
  9. nRF52832 矩阵按键调试 同一列上的按键 任意两个按键 按下 检测不到低电平(电平拉不下来)...
  10. Web终端SSH功能