[Vuejs]-Can't get state stored by Vuex in ~/plugins/axios.js

0👍

Not tested but you could use something like:

~/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = () => ({
  authUser: null,
  token: null
})

const mutations = {
  SET_USER: function (state, user) {
    state.authUser = user
  },
  SET_TOKEN: function (state, token) {
    state.token = token
    instance.defaults.headers = { Authorization: 'Bearer ' + token }
  }
}

const getters = {
 ...
}

const actions = {
  async nuxtServerInit ({ commit }, { req }) {
    ...
  },
  async login ({ commit }, { username, password }) {
    ...
  }
}

export default new Vuex.Store({
  state,
  actions,
  mutations,
  getters,
  modules: {
    ...
  }
});

~/plugins/axios.js

import axios from 'axios'
import VuexStore from '~/store'

var api = axios.create({
  baseURL: 'http://localhost:8000/api/v1/'
})
console.log(VuexStore.state.token) 

export default api

Leave a comment