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

Vue-cookie integration with Vuex

Open antonyhaman opened this issue 5 years ago • 4 comments

Hi there! I'm totally new with Vue and front-end in general (I'm backend java developer :smile:) therefore I might ask dumb questions, but I'm trying to build a simple Vue app with Vuex and cookie as security token. What I'm currently have: main.js:

...
import Vue from "vue";
import vueCookie from "vue-cookies";
...

Vue.component(...);

Vue.use(vueCookie);
new Vue({
  el: "#app",
  router,
  store,
  template: "<App/>",
  components: {App}
}); 

auth.js:

import Vue from "vue";
...

const state = {
  token: this.$cookie.get('token') || "",
  ...
};

/// other auth code

When I run my app I get 'Cannot read property '$cookie' of undefined' at Vue.prototype.$cookie.get('token') line. What I'm doint wrong?

antonyhaman avatar May 27 '20 14:05 antonyhaman

@kotvertolet did you find any solutions ?

abrantes01 avatar Jun 22 '20 13:06 abrantes01

@kotvertolet did you find any solutions ?

Use Vue.cookie.get('token') instead

Tonykaynoni avatar Jul 23 '20 14:07 Tonykaynoni

Vuex doesn't reference the vue instance, to use the instance in vuex you have to use this._vm, so it should work like:

const state = {
  token: this._vm.$cookie.get('token') || "",
  ...
};

I hope this helps you

ramsesgarate avatar Sep 22 '20 21:09 ramsesgarate

If you have declared your import like this

import VueCookie from 'vue-cookie' Vue.use(VueCookie)

you can refer to the global variable with "VueCookie.set()" from anywhere. Even in the Vuex store.

KashyapFero avatar Oct 20 '20 10:10 KashyapFero