Angular 2 在不使用本地存储的情况下记住我的功能

jay*_*tel 3 remember-me angular

有没有什么方法可以在不使用angular 2的本地存储的情况下记住我的功能?

Add*_*Ltd 5

您可以使用 angular2-cookie 记住我的功能。请安装以下插件

npm 安装 angular2-cookie

使用以下链接在您的项目中设置 cookie

https://www.npmjs.com/package/angular2-cookie

组件文件:

public Formdata:any = {};

export class AppComponent {
  constructor(private _cookieService:CookieService) {

    if(_cookieService.get('remember')) {
       this.Formdata.username=this._cookieService.get('username');
       this.Formdata.password=this._cookieService.get('password');
       this.Formdat.rememberme=this._cookieService.get('remember');
    }

  }

  submitData() {
     this._cookieService.put('username',this.Formdata.username);
     this._cookieService.put('password',this.Formdata.password);
     this._cookieService.put('remember',this.Formdat.rememberme);
  }
}
Run Code Online (Sandbox Code Playgroud)

查看文件 :

<form>
  <div>
     <label>Username : </label>
     <input type="text" [(ngModel)]="Formdata.username" />
  </div>
  <div>
     <label>Password : </label>
     <input type="text" [(ngModel)]="Formdata.password" />
  </div>
  <div>
     <input type="checkbox" [(ngModel)]="Formdata.rememberme" /> Remember me 
  </div> 
<button (click)="submitData()">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)

谢谢