简体   繁体   English

关闭和打开应用程序的实时服务器会删除我的本地存储

[英]closing and opening live-server for an app deletes my local storage

I wrote some basic 'To-do' app in JavaScript and HTML, for some reason local storage is deleting after I close and re-open the live-server in terminal.我在 JavaScript 和 HTML 中编写了一些基本的“待办事项”应用程序,由于某种原因,在我关闭并重新打开终端中的实时服务器后,本地存储正在删除。 when i refresh or even close browser and re opens it, everything works just fine.当我刷新甚至关闭浏览器并重新打开它时,一切正常。

just when I close the live-server and re opens it and enters to the browser app, all local storage gone and i start fresh (which is only an array of objects called 'todos')就在我关闭实时服务器并重新打开它并进入浏览器应用程序时,所有本地存储都消失了,我重新开始(这只是一个名为“todos”的对象数组)

While using the app the local storage items are created perfectly and saved perfectly.在使用该应用程序时,本地存储项目被完美创建并完美保存。

this is how I wrote my getSavedTodos (from local storage) and the saveToStorage functions:这就是我编写 getSavedTodos(来自本地存储)和 saveToStorage 函数的方式:

// Fetch existing todos from localStorage
const getSavedTodos= () => {
    const todosJSON= localStorage.getItem('todos')
    if(todosJSON !== null)
    {
        return (JSON.parse(todosJSON))        
    }
    else
    {
        return []
    }
}



//save todos to storage
const saveDataToStorage= (todos) =>
{
    localStorage.setItem('todos', JSON.stringify(todos))
    
}

what am I missing?我错过了什么?

this is my terminal这是我的终端

na:~/Desktop/js/apps$ live-server todo-app
http://0.0.0.0:8080 is already in use. Trying another port.
Ready for changes
Serving "todo-app" at http://127.0.0.1:41709
GET /favicon.ico 404 1.473 ms - 150
^C
na:~/Desktop/js/apps$ live-server todo-app
http://0.0.0.0:8080 is already in use. Trying another port.
Ready for changes
Serving "todo-app" at http://127.0.0.1:38307
GET /favicon.ico 404 2.197 ms - 150
Change detected todo-app/todo-functions.js

The port that is taken (8080) is another app im working on.占用的端口(8080)是我正在开发的另一个应用程序。

Thank you very much!非常感谢!

LocalStorage is unique per origin. LocalStorage 每个来源都是唯一的。 You have to make sure the server always run on same port.您必须确保服务器始终在同一端口上运行。

You can close the app running on 8080. Or simple run the app on another port:您可以关闭在 8080 上运行的应用程序。或者简单地在另一个端口上运行应用程序:

live-server todo-app --port=8081

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

相关问题 在 Visual Studio Code 中编码时如何阻止实时服务器自动打开浏览器? - How to stop live-server opening the browser automatically while coding in Visual Studio Code? 为什么实时服务器会注入这些 javascript 代码 - why is live-server injecting these javascript codes 使用npm live-server时是否清除localStorage? - Does the localStorage get cleared when working with npm live-server? 当我启动 live-server 时,TailwindCss 类没有反映出来 - TailwindCss classes are not reflected when i start live-server 如何在运行实时服务器的情况下在 Visual Studio Code 中调试 JavaScript - How to Debug JavaScript in Visual Studio Code with live-server Running 如何在 VSCode chrome live-server 中运行 npm 模块(puppeteer)? - How to run npm module (puppeteer) in a VSCode chrome live-server? yarn eg:'live-server' 不被识别为内部或外部命令 - yarn eg:'live-server' is not recognized as an internal or external command 关闭并重新启动应用程序后,本地存储中的数据不会持续存在 - data in local storage does not persist after closing and reoping app 是什么导致npm live-server停止检测文件更改? - What would cause npm live-server to stop detecting file changes? npm live-server安装:symlink错误(即使以root / admin身份运行) - npm live-server install: symlink error (even when running as root/admin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM