I am currently in the process of transitioning from localStorage to cookies because I need to work with subdomains. Strangely, when I use this.$cookie.set() or get() in any .vue file, it works perfectly fine. However, when I try to use it in any .js file, I encounter the following error:
TypeError: Cannot read property 'set' of undefined.
Just a heads up, I am using vue-cookie for this implementation.
Below is an excerpt from my store.js file:
import Vue 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'});
commit("authUser", {
userToken: this.$cookie.get('userToken'),
});
.catch(error => console.log(error));
},