简体   繁体   English

如何使用本地存储在不同用户中设置项目?

[英]how to set item in different users using local storage?

 // ! THIS IS FOR SIGN UP PAGE :) const firstName = document.getElementById("firstName"); const lastnName = document.getElementById("lastName"); const email = document.getElementById("newEmail"); const password = document.getElementById("newPassword"); const btnSignup = document.getElementById("btn-signup"); function signUp() { // when mouse click "signup" button const first_name = firstName.value; // getting the value of firstName and so on.. const last_name = lastName.value; const e_mail = newEmail.value; const pass_word = newPassword.value; // if the fields are empty if (!first_name || !last_name || !e_mail || !pass_word) { return alert("you need to fill up all the forms."); } //set user input into JSON let user_data = { firstName: first_name, lastName: last_name, email: e_mail, password: pass_word } //convert to string let user_data_str = JSON.stringify(user_data); //get to localstorage if there is existing user ||or make empty array[] let clientsArr = JSON.parse(localStorage.getItem('users')) || []; // search the list if const userExists = clientsArr.find(user => JSON.stringify(user) === user_data_str); if (userExists) { return alert("User already Exists"); } //push new user to array clientsArr.push(user_data); //save to localStorage localStorage.setItem("users", JSON.stringify(clientsArr)); return alert("Account Created!"); } btnSignup.addEventListener("click", signUp);

this is my functioning code in localStorage(to do list), how can I input different "to do" with different user without changing each other data inside them.?it's tricky for me because I made different users inside the localStorage.这是我在 localStorage(待办事项列表)中的功能代码,如何在不更改彼此内部数据的情况下为不同用户输入不同的“待办事项”。这对我来说很棘手,因为我在 localStorage 中创建了不同的用户。

example:例子:

user1 : to do list 1 user2 : to do list 2 user3 : to do list 3 user1:待办事项1 user2:待办事项2 user3:待办事项3

thanks in advance.!提前致谢。!

You will need some unique identifier to differentiate between users.您将需要一些唯一标识符来区分用户。 You can get that identifier by making a required input or even better, from your backend if you have one.您可以通过进行必要的输入或什至更好的方式从后端获取该标识符(如果有的话)。 With a unique identifier you then just change your .find() method to search for the ID.使用唯一标识符,您只需更改.find()方法即可搜索 ID。

Another option would be to use a different subdomain for each user.另一种选择是为每个用户使用不同的子域。 With each subdomain you get a separate localStorage .对于每个子域,您都会获得一个单独的localStorage But this option may not make sense for your case, it depends on what you want.但是此选项可能对您的情况没有意义,这取决于您想要什么。 I just wanted to list the option.我只是想列出选项。

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

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