【问题标题】:set local storage on submit and and get it if page is refreshed在提交时设置本地存储并在页面刷新时获取它
【发布时间】:2020-09-02 01:42:54
【问题描述】:

我正在尝试在提交表单后在本地存储中设置表单数据。如果页面刷新,我必须在控制台中打印“已提交表单”。我不知道如何写重新加载条件我的代码在下面

submit(){
localStorage.setItem('form-data', JSON.stringify(this.shippingForm.value));
}
window.reload(){
    let formValue = JSON.parse(localStorage.getItem('form-data'));
    console.log("form submitted alreay");
}

【问题讨论】:

  • 您只想检查本地存储是否设置为 no submit() 或者需要在页面加载时检查

标签: angular typescript local-storage


【解决方案1】:

您只需要使用getItem。如果该项目不存在,它将返回 null

if (localStorage.getItem("form-data") === null) {
  // do nothing
} else {
    console.log("form submitted alreay");
}

这里是source,您可以阅读更多相关信息。

在你的情况下,你可以有这样的东西

window.reload(){
if (localStorage.getItem("form-data") === null) {
      // do nothing
    } else {
        // do your other conversions here
        console.log("form submitted alreay");
    }
}

注意:完成后不要忘记清除localstorage

【讨论】:

  • 在我的情况下 window.reload() 也抛出错误。你能告诉我写页面刷新功能吗?
  • 您也可以在准备好的文档中执行此操作..您的页面中是否加载了 jquery ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 2021-08-09
  • 2013-02-11
相关资源
最近更新 更多