when trying to change the use of localStorage to cookies because i need to work with subdomains. When i use this.$cookie.set() or get() in any .vue.js file i can use with no problem but when i use in any .js file i get the following: TypeError: Cannot read property ‘set’ of undefined. when using vue-cookie btw. here is my store.js file: import vue.js from “vue”; import Vuex from “vuex”; import axios from “./axios-auth.js”; import globalAxios from “axios”; import router from “./router”; import VueCookie from ‘vue-cookie’; Vue.use(VueCookie) Vue.use(Vuex); export default new Vuex.Store({ state: { token: null, userId: null, expireIn: null, segmentoSelecionado: null, codigoTransfSelecionado: null }, actions: { login({ commit }, authData) { axios .post(“/auth”, { username: authData.username, password: authData.password, returnSecureToken: true }) .then(res => { const now = new Date().getTime(); const expirationDate = new Date(now + 3500000).getTime(); this.$cookie.set(‘userToken’, res.data.token, {domain: ‘subdomain.domain.com’});
userToken: this.$cookie.get(‘userToken’), }); .catch(error => console.log(error)); }, ### Solution : How it looks to me Vue.use(VueCookie) : extends the vue.js object, adding the$cookie functionality
Vuex does not inherit from Vue, so will not have the $cookie functionality Try using the$cookie on the vue.js object, not the Vuex one: