Обещание Аксиоса не выдает ошибку в методе vue
Как видно из названия, у меня проблема с обнаружением ошибки в методе Vue getTodo
,
Нажмите Установить неверный URL, а затем на Получить Todo, вы можете увидеть, что в магазине произошла ошибка, как и ожидалось, но в Vue, когда getTodo
Promise, then
выполняется, ошибки нет. Если вы выберите Установить правильный URL-адрес, то он работает нормально. Журналы консоли должны выглядеть так:
error on store
error on vue
JavaScript и HTML ниже:
const store = new Vuex.Store({
state: {
todo: '',
msg: '',
url: 'https://jsonplaceholder.typicode.com/todos/test',
correct: false
},
mutations: {
setTodo(state, payload) {
state.todo = payload;
},
setMsg(state, payload) {
state.msg = payload;
},
setCorrect(state) {
state.url = 'https://jsonplaceholder.typicode.com/todos/1';
state.correct = true;
},
setIncorrect(state) {
state.url = 'https://jsonplaceholder.typicode.com/todos/test';
state.correct = false;
}
},
actions: {
getTodo({ state, commit }) {
return axios.get(state.url)
.then(response => {
console.log('success on store');
commit('setMsg', 'success on store');
commit('setTodo', response.data);
})
.catch(error => {
console.log('error on store');
commit('setMsg', 'error on store');
});
}
}
})
new Vue({
el: '#app',
data: {
message: ''
},
computed: {
todo() {
return store.state.todo;
},
msg() {
return store.state.msg;
},
correctUrl() {
return store.state.correct;
}
},
methods: {
getTodo() {
store.dispatch('getTodo').then(() => {
console.log('success on vue');
this.message = 'success on vue'
}).catch((e) => {
console.log('error on vue');
this.message = 'error on vue';
});
},
setCorrect() {
store.commit('setCorrect');
},
setIncorrect() {
store.commit('setIncorrect');
}
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="app">
<p>{{ message }}</p>
<p>{{ todo }}</p>
<p>Correct url: {{ correctUrl }}</p>
<button @click="getTodo">Get Todo</button>
<button @click="setCorrect">Set correct url</button>
<button @click="setIncorrect">Set incorrect url</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuex@3.0.1/dist/vuex.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</body>
</html>
1 ответ
Решение
Вы должны повторно выбросить ошибку после того, как поймаете ее в своем getTodo
действие..
actions: {
getTodo({ state, commit }) {
return axios.get(state.url)
.then(response => {
console.log('success on store');
commit('setMsg', 'success on store');
commit('setTodo', response.data);
})
.catch(error => {
console.log('error on store');
commit('setMsg', 'error on store');
throw error;
});
}
}
const store = new Vuex.Store({
state: {
todo: '',
msg: '',
url: 'https://jsonplaceholder.typicode.com/todos/test',
correct: false
},
mutations: {
setTodo(state, payload) {
state.todo = payload;
},
setMsg(state, payload) {
state.msg = payload;
},
setCorrect(state) {
state.url = 'https://jsonplaceholder.typicode.com/todos/1';
state.correct = true;
},
setIncorrect(state) {
state.url = 'https://jsonplaceholder.typicode.com/todos/test';
state.correct = false;
}
},
actions: {
getTodo({ state, commit }) {
return axios.get(state.url)
.then(response => {
console.log('success on store');
commit('setMsg', 'success on store');
commit('setTodo', response.data);
})
.catch(error => {
console.log('error on store');
commit('setMsg', 'error on store');
throw error;
});
}
}
})
new Vue({
el: '#app',
data: {
message: ''
},
computed: {
todo() {
return store.state.todo;
},
msg() {
return store.state.msg;
},
correctUrl() {
return store.state.correct;
}
},
methods: {
getTodo() {
store.dispatch('getTodo').then(() => {
console.log('success on vue');
this.message = 'success on vue'
}).catch((e) => {
console.log('error on vue');
this.message = 'error on vue';
});
},
setCorrect() {
store.commit('setCorrect');
},
setIncorrect() {
store.commit('setIncorrect');
}
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="app">
<p>{{ message }}</p>
<p>{{ todo }}</p>
<p>Correct url: {{ correctUrl }}</p>
<button @click="getTodo">Get Todo</button>
<button @click="setCorrect">Set correct url</button>
<button @click="setIncorrect">Set incorrect url</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuex@3.0.1/dist/vuex.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</body>
</html>