link274 link275 link276 link277 link278 link279 link280 link281 link282 link283 link284 link285 link286 link287 link288 link289 link290 link291 link292 link293 link294 link295 link296 link297 link298 link299 link300 link301 link302 link303 link304 link305 link306 link307 link308 link309 link310 link311 link312 link313 link314 link315 link316 link317 link318 link319 link320 link321 link322 link323 link324 link325 link326 link327 link328 link329 link330 link331 link332 link333 link334 link335 link336 link337 link338 link339 link340 link341 link342 link343 link344 link345 link346 link347 link348 link349 link350 link351 link352 link353 link354 link355 link356 link357 link358 link359 link360 link361 link362 link363 link364 link365 link366 link367 link368 link369 link370 link371 link372 link373 link374 link375 link376 link377 link378 link379 link380 link381 link382 link383 link384 link385 link386 link387 link388 link389 link390 link391 link392 link393 link394 link395 link396 link397 link398 link399 link400 link401 link402 link403 link404 link405 link406 link407 link408 link409 link410

[Vue.js] Can't use cookies in Store.js file Subscribe to RSS

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’});
commit(“authUser”, {
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:

Vue.$cookie.set(‘userToken’, res.data.token, {domain: ‘subdomain.domain.com’});