Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue-cookie integration with Vuex #52

Open
antonyhaman opened this issue May 27, 2020 · 4 comments
Open

Vue-cookie integration with Vuex #52

antonyhaman opened this issue May 27, 2020 · 4 comments

Comments

@antonyhaman
Copy link

antonyhaman commented May 27, 2020

Hi there! I'm totally new with Vue and front-end in general (I'm backend java developer 😄) 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?

@abrantes01
Copy link

@kotvertolet did you find any solutions ?

@Tonykaynoni
Copy link

@kotvertolet did you find any solutions ?

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

@ramsesgarate
Copy link

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

@KashyapFero
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants