简体   繁体   English

Nuxt.js app中Vuex store设置值的方法

[英]How to set values in Vuex store in Nuxt.js app

I have never used Vuex with Nuxt.js , so I have met the problem.我从未将VuexNuxt.js一起使用,所以我遇到了这个问题。 Here is my store/index.js file:这是我的store/index.js文件:

export const state = () => ({
  wrongCredentials: false,
  logged: false,
  uxd: null
})

export const mutations = {
  setWrongCredentials(state, value) {
    state.wrongCredentials = value
  },
  setLogged(state, value) {
    state.logged = value
  },
  setUxd(state, value) {
    state.uxd = value
  },
}

As you can see there is state and mutations.如您所见,有 state 和突变。 In my other file, where I check user's JWT token and, depending on resultM I want set values in store:在我的另一个文件中,我检查了用户的 JWT 令牌,并且根据 resultM 我想在存储中设置值:

import jwt from 'jsonwebtoken'
import cert from '../jwt/public'

export default {
  verify (token) {
    jwt.verify(token, cert, async (err, decoded) => {
      if (err) {
        this.$store.state.wrongCredentials = true
        this.$store.state.logged = false
        this.$store.state.uxd = null
      } else {
        this.$store.state.wrongCredentials = false
        this.$store.state.logged = true
        this.$store.state.uxd = decoded.uxd
      }
    })
  }
}

The code you see doesn't work correctly, it just doesn't set values, so, I have created mutations and did something like this:你看到的代码不能正常工作,它只是没有设置值,所以,我创建了突变并做了这样的事情:

await this.$store.dispatch('setWrongCredentials', true)

Also doesn't work.也不起作用。 The problem is, I don't know how to work with Vuex store not in .vue files, so, how can I set values in store?问题是,我不知道如何使用不在.vue文件中的Vuex存储,那么,如何在存储中设置值?

Unfortunately, I have not found solution of this problem like I wanted in .js file, so, here is another solution.不幸的是,我还没有找到我想要的.js文件中的这个问题的解决方案,所以,这是另一个解决方案。

First of all, you need to set no only state and mutations, but also actions:首先,您不仅需要设置 state 和突变,还需要设置操作:

export const actions = {
  fetchWrongCredentials(ctx, value) {
    ctx.commit('setWrongCredentials', value)
  },
  fetchLogged(ctx, value) {
    ctx.commit('setLogged', value)
  },
  fetchUxd(ctx, value) {
    ctx.commit('setUxd', value)
  }
}

And in your Vue component you need to set values in state using actions, just like that:在您的Vue组件中,您需要使用操作在 state 中设置值,就像这样:


  methods: {
    ...mapActions(['fetchWrongCredentials', 'fetchLogged', 'fetchUxd']),
    async login() {
      await login({
        password: this.userPassword,
        login: this.userLogin,
        twoFactor: this.twoFactor
      }).then(result => {
        const jwtRes = jwt.verify(result.token)
        this.fetchLogged(true)
        this.fetchWrongCredentials(false)
        this.fetchUxd(jwtRes.token)
      }).catch(() => {
        this.fetchLogged(false)
        this.fetchWrongCredentials(true)
        this.fetchUxd(null)
        this.error = true
        setTimeout(() => {
          this.error = false
        }, 1000)
      })
    }
  }

In my case, I had to modify that .js file:就我而言,我必须修改那个.js文件:

import jwt from 'jsonwebtoken'
import cert from '../jwt/public'

export default {
  verify (token) {
    return jwt.verify(token, cert, (err, decoded) => {
      if (err) {
        return false
      } else {
        return { token: decoded.uxd }
      }
    })
  }
}

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

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