有没有办法在角度2中使用记住我功能而不使用本地存储空间?
答案 0 :(得分:4)
你可以使用angular2-cookie记住我的功能。 请安装以下插件
npm install angular2-cookie
使用以下链接
在您的项目中设置Cookiehttps://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>
由于