简体   繁体   English

我怎样才能将对象存储在本地存储中的角度?

[英]How can i store object in local storage in angular?

i have an object named model i want it to store in localstorage . 我有一个名为model的对象,我希望它存储在localstorage中。

model.username and model.password are two fields of my form. model.username和model.password是我表单的两个字段。 i want to store model.username in local storage. 我想将model.username存储在本地存储中。 i need to add every username to be added in local storage. 我需要添加要添加到本地存储中的每个用户名。 But in my case every time i tried entering new username the previous username get overwritten in local storage ie only one username is only stored in ls anytime. 但是就我而言,每次我尝试输入新用户名时,先前的用户名都会在本地存储中被覆盖,即,任何时候都只能在ls中存储一个用户名。

Following is the login template 以下是登录模板

<form  (ngSubmit)="f.valid && onSubmit(f) " #f="ngForm" novalidate>
  <div>
  <label for="username">Username</label>
          <input type="text"  name="username" [(ngModel)]="model.username" #username="ngModel" required appForbiddenName="jimit" />
          <div *ngIf="f.submitted && !username.valid" >Username is required</div>
          <div *ngIf="username.errors.forbiddenName">
            Username has already taken.
          </div>

  </div>
  <div>
    <label for="password">Password</label>
          <input type="password"  name="password" [(ngModel)]="model.password" #password="ngModel" required />
          <div *ngIf="f.submitted && !password.valid">Password is required</div>
  </div>      
  <div>    
         <button type="submit" >Login</button></div>

  <div>
    <button type="reset">Clear</button>
  </div>

  </form>

  <button (click)="onRegister()">Register</button>

Following is the login component 以下是登录组件

import { Component, OnInit } from '@angular/core';
import { FormBuilder, NgForm } from '@angular/forms';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(private router: Router) {
    }

 model: any = {};

onSubmit(form: NgForm) {

  console.log(form.value.username);
  console.log(this.model.username);
  localStorage.setItem ('username', this.model.username);
  this.router.navigate(['/home']);
}

onRegister() {
  this.router.navigate(['/register']);
  window.alert('please wait while we process your request');
}


  ngOnInit() {
  }

Ignoring the strange idea of storing multiple usernames in localStorage , you can do this by inserting an array or object. 忽略在localStorage中存储多个用户名的奇怪想法,您可以通过插入数组或对象来实现。 Because the keys of localStorage are unique: 由于localStorage的键是唯一的:

const usernames = JSON.parse(localStorage.getItem('usernames') || '[]');

if (!usernames.includes(this.model.username)) {
  usernames.push(this.model.username);
  localStorage.setItem('usernames', JSON.stringify(usernames));
}

If however you are doing this to get the data later after navigating, I suggest you have a thorough read about using services/store instead of localStorage. 但是,如果您这样做是为了在导航后稍后获取数据,则建议您全面阅读有关使用服务/存储而不是localStorage的信息。

本地存储基于键存储值,并且一次仅保存一个值,而不保存数组。

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

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