Answer the question
In order to leave comments, you need to log in
How to refactor work with errors?
Hey! There is a repeating code from view to view.
export default {
data() {
return {
form: {
name: null,
dateFrom: null,
},
error: null,
errors: []
}
},
methods: {
create() {
this.error = null;
this.errors = [];
axios
.post('/events/create', this.form)
.then(() => {
this.$router.push({name: 'events'});
})
.catch(error => {
if (error.response) {
if (error.response.data.error) {
this.error = error.response.data.error;
} else if (error.response.data.errors) {
this.errors = error.response.data.errors;
}
} else {
/* console.log(error.message);*/
}
});
}
}
}
.catch(error => {
if (error.response) {
if (error.response.data.error) { //если ошибка от Exeption
this.error = error.response.data.error;
} else if (error.response.data.errors) { // Если ошибки валидации формы
this.errors = error.response.data.errors;
}
} else {
/* console.log(error.message);*/
}
});
create() {
this.error = null;
this.errors = [];
...}
data() {
return {
.....
error: null,
errors: []
}
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question