简体   繁体   English

Nuxt.js vuex 存储未持久化

[英]Nuxt.js vuex store not persisted

I've got some strange issues with my Nuxt.js Setup.我的 Nuxt.js 设置有一些奇怪的问题。 Some States in Store arent persistent, everytime I load another view, they went back to the default value. Store 中的某些状态不是持久的,每次我加载另一个视图时,它们都会返回默认值。

pages/test.vue页面/test.vue

<template>
  <section class="section">
    <b-container>
      <b-row>
        <b-col cols=12>
          <b-button @click="setTest" variant="dark">test</b-button> | {{this.$store.state.test}} |
        </b-col>
      </b-row>
    </b-container>
  </section>
</template>

<script>
export default {
  name: 'test',
  methods: {
    setTest() {
      this.$store.commit("setTest")
    },
  }
}
</script>

store/index.js存储/index.js

export const state = () => ({
  test: "test"
})

export const mutations = {
  setTest: (state) => state.test = 'Hello'
}

Testscenario is to hit the "test"-button who call the method with mutation-commit "setTest" which set the state to "Hello".测试场景是点击“测试”按钮,该按钮使用突变提交“setTest”调用该方法,该方法将 state 设置为“Hello”。 Currently it works fine, but if I changed the view or reload the page, the state is set to default "test".目前它工作正常,但如果我更改视图或重新加载页面,state 将设置为默认“测试”。

What am I doing wrong?我究竟做错了什么?

Alright, so the behavior is totally logic.好吧,所以行为完全是合乎逻辑的。 Vuex is not supposed to be persistent. Vuex 不应该是持久的。

For any persistence on the front-end, you need either:对于前端的任何持久性,您需要:

  • cookies cookies
  • localStorage本地存储
  • pass it in the URL (query params)在 URL 中传递它(查询参数)
  • IndexedDB 索引数据库
  • get the data back from making a call to a backend通过调用后端获取数据

Some of the packages here may be useful: https://github.com/vuejs/awesome-vue#persistence这里的一些包可能有用: https://github.com/vuejs/awesome-vue#persistence


If you reload your page with an F5, all your JS will be wiped and loaded again.如果您使用 F5 重新加载页面,您的所有 JS 将被擦除并再次加载。 Hence, no state will be kept since it will be a brand new page.因此,不会保留 state ,因为它将是一个全新的页面。 When working with frontend frameworks, you cannot expect it to just work after a page refresh.使用前端框架时,您不能期望它只在页面刷新后才能工作。

Same go when you do follow an href , it is an actual real navigation.当您跟随href时,同样的 go 是真正的导航。 What you need to do, is to use a <nuxt-link></nuxt-link> component, with something like to="/profile" to let VueJS move to this URL.您需要做的是使用<nuxt-link></nuxt-link>组件,使用类似于to="/profile"的内容让 VueJS 移动到这个 URL。

NuxtLink is a Nuxt.js component and essentially a component on top of <router-link></router-link> , which is Vue router . NuxtLink是一个 Nuxt.js 组件,本质上是<router-link></router-link>之上的一个组件,它是Vue 路由器

TLDR: you cannot use things like window.location.href , nor <a href="..." . TLDR:您不能使用window.location.href类的东西,也不能使用<a href="..." You may use the given components by either Nuxt ( nuxt-link ) or Vue's router ( router-link ), if you're using VueJS only.如果您仅使用 VueJS,您可以通过 Nuxt ( nuxt-link ) 或 Vue 的路由器 ( router-link ) 使用给定的组件。

Giving a read to the Vue router's documentation may be a good start to understand things a bit more !阅读 Vue 路由器的文档可能是了解更多内容的良好开端!


If you're using nuxt/auth , give a try to that one: https://auth.nuxtjs.org/api/storage/#universal-storage如果您使用的是nuxt/auth ,请尝试一下: https://auth.nuxtjs.org/api/storage/#universal-storage

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

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