vue-session icon indicating copy to clipboard operation
vue-session copied to clipboard

Can I access to 'this.$session' on Vuex store?

Open kotehector opened this issue 6 years ago • 8 comments

kotehector avatar Apr 17 '18 16:04 kotehector

Same question here...

mbana avatar May 23 '18 15:05 mbana

same question

gamcoh avatar Jun 05 '18 18:06 gamcoh

Ok I figured it out. Here we go. In main.js

import VueSession from 'vue-session'
Vue.use(VueSession, {persist: true})

In Store.js or store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let store =  new Vuex.Store({
    state: {
        isLoggedIn: false,
    },
    getters: {
        isLoggedIn:function(state){
            return state.isLoggedIn;
        },
        user:function(state){
            // console.log(store);
            return store._vm.$session.get('user');
        }
    },
    mutations: {
        saveUser(state,payload){
            console.log('user payload', payload);
            // this._vm.$session.start();
            store._vm.$session.set('user',payload);
            state.isLoggedIn = (payload != null)
        }
    },
    actions: {
        
    }
});

export default store

Pyronerd62 avatar Jul 03 '18 19:07 Pyronerd62

The point is to avoid having to write such logic. In reply to: https://github.com/victorsferreira/vue-session/issues/14#issuecomment-402272269.

mbana avatar Jul 04 '18 08:07 mbana

I'm not sure what you're asking. That is your basic Vuex store setup. How else do you want to use the mutations and getters? This works for us as the session is tied to the Vue instance itself and it persists across tabs and components.

Pyronerd62 avatar Jul 04 '18 16:07 Pyronerd62

I managed to access it with Vue.prototype.$session, not sure if this is the "right" way to do it.

shlomiLan avatar Apr 26 '20 11:04 shlomiLan

import Vue from 'vue';
import Vuex from 'vuex';
import VueSession from 'vue-session';

Vue.use(Vuex);
Vue.use(VueSession);

export const store = new Vuex.Store({
    state: {
       isLoggedInSession: !!Vue.prototype.$session.get('access-token')
    }
});

aswzen avatar Jul 23 '20 01:07 aswzen

You can only use this.$session.get() or any method on VueSessionStorage when you are in Vue components. But in other files use the following. First make it global in main.js by import VueSessionStorage from 'vue-session' Vue.use(VueSessionStorage) then in your API or Vuex file

import Vue from 'vue' import VueSessionStorage from 'vue-session'

Vue.prototype.$session.get("token")

abdulmalikgh avatar Aug 05 '21 12:08 abdulmalikgh