If I using ajax in vue component like this :
It works. I successfully get the file
But here I want to using vuex store
My pattern of vuex store like this :
I change my vue component like this :
<template>
<div>
...
</div>
</template>
<script>
export default {
methods: {
submitForm() {
...
formData.append('file', files)
this.updateProfile({formData, reload: true})
}
}
}
</script>
In the component vue, it will call updateProfile method in modules user
The modules user like this :
import { set } from 'vue'
import users from '../../api/users'
import * as types from '../mutation-types'
// initial state
const state = {
addStatus:null
}
// getters
const getters = {
updateProfileStatus: state => state.addStatus,
}
// actions
const actions = {
updateProfile ({ dispatch,commit,state },{user,reload})
{
users.updateProfile(user,
response => {
commit(types.UPDATE_PROFILE_SUCCESS)
},
errors => {
commit(types.UPDATE_PROFILE_FAILURE)
}
)
}
}
// mutations
const mutations = {
[types.UPDATE_PROFILE_SUCCESS] (state){
state.addStatus = 'success'
},
[types.UPDATE_PROFILE_FAILURE] (state){
state.addStatus = 'failure'
}
}
export default {
state,
getters,
actions,
mutations
}
From the modules user, it will call updateProfile method in api users
The api users like this :
import Vue from 'vue'
export default {
updateProfile(user, cb, ecb = null) {
axios.post('/member/profile/update', user)
.then(response => cb(response))
.catch(error => ecb(error))
}
}
So my pattern like that. So i'm using pattern of vuex store
My problem here is I'm still confused to send file data
If I console.log(user)
in the updateProfile method on modules user, the result is undefined
How can I send file data using vuex store?
The issue is with this line:
The
updateProfile
action is expecting to receive an object withuser
andreload
properties, but you are passing aformData
property instead ofuser
.Simply pass the
user
property instead:So if Im using Vuex I use Store. If you dont intend to then this answer may not help you. If you are running into issues I would first try storing something like a string because wether its a file or a string, the store functionality should not matter. Once a string is setup , then move to a file (blob , string or arr whatever... ).
Vue has great docs, though things still can be tricky, its good read a lil and have a reference. Here
I use Vuex.Store
then define your store up like this:
now you can save your file like so
That saves the state. Whats cool is Vue gives you a store variable you can set in the component by passing to the Vue instance:
Then you can reference by calling this.$store . later you can use this.$store.getters.file to retrieve the file var. You can stop there (call this.$store.commit) , but note we are not using actions here . You can add actions which add another layer between setting and mutating state.
more on them here
if you want to use actions add something like this where you commit to save the data
and you use dispatch to call an action on a Store
so Store.dispatch('SAVE_FILE',{value:file})
hopefully that helps