正如标题所述,我在Vue方法getTodo
中捕获错误时遇到问题。
点击设置错误的网址,然后点击获取待办事项,您会发现商店中出现了预期的错误,但是在Vue中,当getTodo
许诺的{ {1}}被执行,没有错误。如果您选择设置正确的网址,则可以正常运行。
控制台日志应如下所示:
then
下面的JavaScript 和 HTML :
error on store
error on vue
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');
}
}
})
答案 0 :(得分: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>