简体   繁体   English

Vue.Js, Nuxt.js 多步骤表单 - 如何在页面重新加载时保存数据

[英]Vue.Js, Nuxt.js Multi-steps form - how to presist data on page reload

I have a 3-steps form, Now I'm using 3 diffrenet pages for each step.我有一个 3 步表格,现在我为每个步骤使用 3 个不同的页面。 each step requires a submit to the backend for doing some validations and calculations that is then send to the next step.每个步骤都需要向后端提交以进行一些验证和计算,然后将其发送到下一步。

The problem is, say that a use filled the first step and go to the second step, if he reloads the page on the 2nd step, the data from the first step will be removed from the Vuex store, and on the last step when he submit the form he will get an error because that data from the first step is no longer availabe on the Vuex store.问题是,假设一个用户填写了第一步,go 到了第二步,如果他在第二步重新加载页面,第一步的数据将从 Vuex 存储中删除,而在最后一步当他提交表单他会得到一个错误,因为第一步的数据在 Vuex 商店中不再可用。 also If he press the back button on any step, the form on the previous step will be cleared out and the data will not be there.如果他在任何一步按下后退按钮,上一步的表格将被清除,数据将不存在。

I can use localStorage and save the data for each step on it, but the problem with this appraoch is some data are sensitive and I don't want to store sensitive data on localStorage.我可以使用 localStorage 并保存每个步骤的数据,但是这种方法的问题是某些数据是敏感的,我不想将敏感数据存储在 localStorage 上。 the second problem with this approach is the data will always be in the form and will not be cleared if the user closes the browser or anything like that.这种方法的第二个问题是数据将始终在表单中,如果用户关闭浏览器或类似的东西,数据将不会被清除。

What is a good way to fix this issues?解决此问题的好方法是什么?

As an alternate to localStorage, session storage can be used.作为 localStorage 的替代品,可以使用 session 存储。

Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.每当在浏览器的特定选项卡中加载文档时,都会创建一个唯一页面 session 并将其分配给该特定选项卡。 That page session is valid only for that particular tab.该页面 session 仅对该特定选项卡有效。

  • A page session lasts as long as the tab or the browser is open, and survives over page reloads and restores.只要选项卡或浏览器打开,页面 session 就会持续存在,并且在页面重新加载和恢复后仍然存在。

  • Opening a page in a new tab or window creates a new session with the value of the top-level browsing context, which differs from how session cookies work. Opening a page in a new tab or window creates a new session with the value of the top-level browsing context, which differs from how session cookies work.

  • Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.使用相同的 URL 打开多个选项卡/窗口会为每个选项卡/窗口创建 sessionStorage。

  • Duplicating a tab copies the tab's sessionStorage into the new tab.复制选项卡会将选项卡的 sessionStorage 复制到新选项卡中。 Closing a tab/window ends the session and clears objects in关闭选项卡/窗口结束 session 并清除对象
    sessionStorage.会话存储。

Taken form https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage 取自 https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

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

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