【问题标题】:Angular 2 remember me functionality without using localstorageAngular 2 在不使用本地存储的情况下记住我的功能
【发布时间】:2018-04-18 10:44:17
【问题描述】:

有什么方法可以让 记住我 功能而不使用 angular 2 中的本地存储

【问题讨论】:

标签: angular remember-me


【解决方案1】:

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

npm install 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);
  }
}

查看文件:

<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>

谢谢

【讨论】:

  • “CookieService”类型上不存在属性“set”。我在 cookieservice 中得到这个
  • 在 Angular6 中无法使用 .put。它与.set 合作。谢谢!
  • 但这不是很好。如果我用account1 登录,然后用account2 登录,它不会记住他们两个的密码.. 只是我最后一次登录的密码,即account2。这是您想要实现的目标还是有其他解决方案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2015-07-13
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
相关资源
最近更新 更多