Skip to content

Instantly share code, notes, and snippets.

@vanholler
Last active October 31, 2020 23:51
Show Gist options
  • Save vanholler/0fb75329cbb5f05992e4253ad6c8b43f to your computer and use it in GitHub Desktop.
Save vanholler/0fb75329cbb5f05992e4253ad6c8b43f to your computer and use it in GitHub Desktop.
// in Actions:
uploadFile: ({ state, commit }, { file }) => {
return new Promise((resolve, reject) => {
Fetcher({
url: endpoints.uploadFile,
method: 'POST',
headers: {
'Authorization': `${state.token}`
},
onUploadProgress ({ loaded, total }) {
const uploadProgress = Math.round(loaded / total * 100)
commit('setPercentage', uploadProgress)
},
data: {
file
}
})
.then((resp) => {
resolve(resp)
})
.catch((err) => {
console.log('upload error,', err)
reject(err.response)
})
})
}
// in muttations
setPercentage (state, num) {
state.percentage = num
}
// in state
percentage: ''
// in component
computed: {
percentage () {
return this.$store.state.percentage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment