, , . Vuex . Vuex , , . Vuex, Vuex persisted state, localStorage. , localStorage. , localStorage . - .
, , Vuex:
methods: {
login () {
axios.post('/api-token-auth/login/', {
username: this.username,
password: this.pwd1
}).then(response => {
that.$store.commit('LOGIN_SUCCESS', response)
}).catch(error => {
console.log("Error login")
console.log(error)
})
this.dialog = false
}
}
Vuex (,/store/modules/user.js ):
Vue.use(Vuex)
const state = { token: null}
const mutations = {
LOGIN_SUCCESS(state, response) {
state.token = response.token
}
export default {
state,
mutations
}
Getter, :
this.$store.state.user.token
, Vue. , main.js :
import store from './store/index.js'
new Vue({
el: '#app',
store
})