最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I keep the name of user logged vue.js - Stack Overflow

programmeradmin7浏览0评论

Im trying to store the user logged on my application. Im using a store.js file to use vuex and save my variables.

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import Cookies from 'js-cookie'


Vue.use(Vuex)

export const store = new Vuex.Store({
  state: {
    userloged: ''
  }
})

I declared the store variable on my main.js and I used in this way, when I save the name of the user in my login ponent I use,

        this.$store.state.userloged = this.username;

And when Im going to used in the others ponents I got it in this way,

 puted:{
    userloged() {
        return this.$store.state.userloged;
    }
},

But if I refresh the page I lost the information. What can I do?

Im trying to store the user logged on my application. Im using a store.js file to use vuex and save my variables.

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import Cookies from 'js-cookie'


Vue.use(Vuex)

export const store = new Vuex.Store({
  state: {
    userloged: ''
  }
})

I declared the store variable on my main.js and I used in this way, when I save the name of the user in my login ponent I use,

        this.$store.state.userloged = this.username;

And when Im going to used in the others ponents I got it in this way,

 puted:{
    userloged() {
        return this.$store.state.userloged;
    }
},

But if I refresh the page I lost the information. What can I do?

Share Improve this question asked May 15, 2018 at 16:39 Camila NieblesRCamila NieblesR 932 silver badges7 bronze badges 1
  • I added an example on my answer – ricardoorellana Commented May 15, 2018 at 19:24
Add a ment  | 

3 Answers 3

Reset to default 3

You should use 'vuex-persistedstate' to persist Vuex state with localStorage.

You should update the state through mutations and dispatching an action, redifine your vuex instance to contain the following objects:

import createPersistedState from 'vuex-persistedstate';

const store = new Vuex.Store({
  state: {
    userlogged: ''
  },
  mutations: {
    saveUserLogged (state, loggedUser) {
      state.userLogged = loggedUser
    }
  },
  actions: {
    saveUserLogged (context, loggedUser) {
      context.mit('saveUserLogged', loggedUser)
    }
  },
  plugins: [createPersistedState()]
})

So to save the loggedUser you should dispatch an action:

this.$store.dispatch('saveUserLogged', this.username);

You can learn more about mutations and actions in the Vuex official site

Please take a look to this example https://codesandbox.io/s/0yy7vk29kv

Vuex doesn't persist the state over page reloads.

You have to use something like Vuex-persistedstate plugin.

import createPersistedState from 'vuex-persistedstate'

const store = new Vuex.Store({
  // ...
  plugins: [createPersistedState()]
})

Docs and instalation instructions: https://www.npmjs./package/vuex-persistedstate

You can simply use the cookies with https://www.npmjs./package/vue-cookies

发布评论

评论列表(0)

  1. 暂无评论