简体   繁体   English

使用 VueJs/Vuex 重新加载页面时保持持久状态

[英]Maintaining persistent state when reloading a page using VueJs/Vuex

I am trying to maintain state derived from my data object and maintain that state upon refreshing the page.我试图维护从我的数据对象派生的状态,并在刷新页面时维护该状态。 The api is connected via the filter function in getSpecificBeer(). api 通过 getSpecificBeer() 中的过滤器函数连接。

 <template> <div class="mx-auto w-50 d-inline-block w-responsive" :adaptive="true" :resizable="true"> <div class="beerCard"> <div class="card" > <div class="card-body"> <h3 class="card-title">{{ beer.name }}</h3> <h5 class="card-title">{{ beer.brand }}</h5> <h5 class="card-title">{{ beer.description }}</h5> <h5 class="card-title">{{ beer.ABV }}</h5> </div> </div> <div class="review" style="padding-top: 2em"> <Review /> </div> </div> </div> </template> <script> // import { mapActions } from 'vuex' import Review from '../components/Review.vue'; export default { components: { Review }, name: "BeerView", props: ["beerId",'beers'], data() { return { beer: { name: '', id: '', brand: '', ABV: '', description: '' }, }; }, mounted() { this.getSpecificBeer() }, methods: { getSpecificBeer() { this.beer = this.beers.filter((beer) => beer.id == this.$route.params.beerId)[0]; this.$store.state.setBeerView this.$store.commit('setBeerView', this.beerView) console.log(this.beerBeerView) }, } }; </script>

I am trying to save the data to the local storage utilising vuex and createPersistedState.我正在尝试使用 vuex 和 createPersistedState 将数据保存到本地存储。 I can see the empty beerView object in the application section of devtools, but nothing is committed and I loose all the data upon refreshing the page.我可以在 devtools 的应用程序部分看到空的 beerView 对象,但没有提交任何内容,刷新页面时我丢失了所有数据。

 const state = { beerView: {} } const mutations = { setBeerView: (state, beerView) => {state.beerView = beerView} } export default { state, mutations }

 import Vue from 'vue'; import Vuex from 'vuex'; import createPersistedState from "vuex-persistedstate"; import beerView from './modules/beerView' Vue.use(Vuex) const beerViewState = createPersistedState({ paths: ['beerView'] }) const store = new Vuex.Store({ modules: { beerView }, plugins: [beerViewState] }) export default store

You can try to use local storage.您可以尝试使用本地存储。 To set an item you can use:要设置一个项目,您可以使用:

localStorage.setItem('key','value');

Note: if you want the value to be JSON or object you would have to use JSON.stringify on the JSON or object and when you get the value you ca注意:如果您希望该值为 JSON 或对象,则必须在 JSON 或对象上使用 JSON.stringify,当您获得该值时,您可以

To get an item you can use:要获得物品,您可以使用:

var taste = localStorage.getItem('key');
// value

To remove an item you can use:要删除项目,您可以使用:

localStorage.removeItem('favoriteflavor');

You can view the local storage in the dev tools.您可以在开发工具中查看本地存储。 Open dev tools and then go to Application-->Local Storage-->[the domain that made the data] and then you can see the keys and values and you can edit them.打开开发工具,然后转到应用程序-->本地存储-->[生成数据的域],然后您可以看到键和值,您可以对其进行编辑。

Note: This works in normal js too.注意:这也适用于普通 js。 There is probably a way to do it just for vue but I don't know much about Vuex可能有一种方法可以为 vue 做到这一点,但我对 Vuex 了解不多

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

相关问题 VueJs 使用 store 或 vuex 时出现空白页面 - VueJs got a blank page when using store or vuex VueJS-Vuex状态更新时组件未更新 - VueJS - Components not updating when Vuex state updated 在浏览器上访问/重新加载/或返回页面时,如何使主题 state 持久化? - How to make theme state be persistent when visiting/reloading/or going back a page on the browser? 使用vuex-persistedstate的持久状态:当我在另一个标签中更改状态时,第一个标签中的存储状态不会更新 - Persistent state using vuex-persistedstate: store state in first tab doesnot update when i mutate state in another tab 如何在另一个路由器页面 VUEJS、VUEX 中访问我的数组 state - How to access my state of array in another router page VUEJS, VUEX 在 VueJS 项目上使用 Vuex 得到错误“TypeError: this.$state is undefined” - Getting error "TypeError: this.$state is undefined" using Vuex on VueJS project Vuejs / Vuex如何使用突变在vuex state中的对象数组中删除(过滤)Object - Vuejs/Vuex How to remove(filter) an Object inside an Array of Objects in vuex state using mutations Vuex 在使用操作时不更新 state - Vuex not updating state when using actions 页面刷新时的 Vuex 状态 - Vuex state on page refresh 使用 vuejs 或 vuex 提交表单时,formdata.append 不起作用 - formdata.append not working when submiting a form using vuejs or vuex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM