简体   繁体   English

在 Vue JS 中,如何在页面刷新时存储组件的“数据”(真/假)?

[英]In Vue JS, how do I store the "data" (true/false) of a component on a page refresh?

Using Vue Js with Laravel I would like to keep open an expanded nav bar when the user refreshes the page (if they have chosen to open it initially).在 Laravel 中使用 Vue Js 我想在用户刷新页面时保持打开一个扩展的导航栏(如果他们最初选择打开它)。

The nav bar open/close status is stored in the component's data with a true/false boolean.导航栏打开/关闭状态以 true/false 布尔值存储在组件的数据中。

I'm a bit confused of which approach to take as have researched various options, so seek the best advice as a noob.由于研究了各种选择,我对采用哪种方法有点困惑,所以作为菜鸟寻求最好的建议。 Ideally there is a simple way to have the 'data' persist in my component rather than it getting re-rendered to the default of false!理想情况下,有一种简单的方法可以让“数据”保留在我的组件中,而不是重新渲染为默认值 false! Therefore I guess it needs storing in the user's "session" state locally right?因此我想它需要在本地存储在用户的“会话”状态中,对吗?

But what do I use and how?但是我使用什么以及如何使用?

Thanks.谢谢。

Vuex with a plugin such as this one if your application is large/is growing in complexity.如果您的应用程序很大/越来越复杂,则 Vuex 可以使用这样的插件。 Its a bit of a learning curve though so if you only have a few things to keep track of their states, then localstorage would be a good solution.虽然它有点学习曲线,所以如果你只有几件事来跟踪它们的状态,那么 localstorage 将是一个很好的解决方案。

Localstorage has the advantage of being easy to use, widely adopted and if you need to in the future, it integrates nicely with state management systems like vuex. Localstorage 的优点是易于使用,被广泛采用,如果您将来需要,它可以很好地与 vuex 等状态管理系统集成。 The api is quite simple, really just getItem and setItem for common simple use cases.该 api 非常简单,实际上只是getItemsetItem用于常见的简单用例。 It persists across browser sessions as well.它也会在浏览器会话中持续存在。

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Sessionstorage is roughly equivalent to localstorage with the key difference being once you close the browser, it gets wiped. Sessionstorage 大致相当于 localstorage,主要区别在于一旦关闭浏览器,它就会被擦除。

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

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