I wanted to move axios request logic to separate service in my Vue.js app. Axios always returns promise, how can I fetch response data from it in my component? Or may be there are some other solution for this?
UserService.js
class UserService {
getUser() {
const token = localStorage.getItem('token');
return axios.get('/api/user', {
headers: {
'Authorization': 'Bearer ' + token
}
}).then(function (response) {
return response.data;
}).catch(function (error) {
console.log(error);
});
}
getUserTasks() {
const token = localStorage.getItem('token');
return axios.get('/api/user/task', {
headers: {
'Authorization': 'Bearer ' + token
}
}).then(response => {
return response;
}).catch(function (error) {
console.log(error);
});
}
get currentUser() {
return this.getUser();
}
}
export default new UserService();