最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在 nodejs 中使用 LocalStorage

网站源码admin35浏览0评论

如何在 nodejs 中使用 LocalStorage

如何在 nodejs 中使用 LocalStorage

我知道这个问题在 stackoverflow 中被问过很多次,但似乎没有一个能帮助我解决我的问题。谁能帮我提出任何建议或答案会有所帮助。我有点卡在这部分了。

这是我的代码

``const getItemFromLocalStorage = (key) =\> {
 let value;
 if (typeof window !== "undefined") {
  value = window.localStorage.getItem(key)
  ? JSON.parse(
  window.localStorage.getItem(key)
  ): null;
 } else {
  console.log(
  "Get Item You are on the server can not use window.localStorage"
 ); }
 return value;
};

const setItemToLocalStorage = (key, value) =\> {
 if (typeof window !== "undefined") {
  window.localStorage.setItem(key, value);
 } else {
  console.log(
   "Set Item You are on the server can not use window.localStorage"
  );
 }
};

const removeItemFromLocalStorage = (key) =\> {
 if (typeof window !== "undefined") {
  window.localStorage.removeItem(key);
 } else {
  console.log(
   "Remove Item You are on the server can not use window.localStorage"
  );
 }
};

const clearLocalStorage = () =\> {
 if (typeof window !== "undefined") {
  window.localStorage.clear();
 } else {
  console.log(
   "Clear Item You are on the server can not use window.localStorage"
  );
 }
};`
`

请帮我解决这个问题

回答如下:
发布评论

评论列表(0)

  1. 暂无评论