【发布时间】:2021-06-27 05:30:34
【问题描述】:
我在 Promise 对象上使用“then”和“catch”函数,而不使用异步函数来实现简单和可管理的响应部分,而不是使用 await 和条件 if-else 然后检查状态 if 不是服务器端错误但是我对如何管理 Promise Object 的“then”方法以自动刷新已通过创建函数获取并使用过滤器在“then”方法中重新定义的数据而不再次获取后端的数据有点困惑.
export default {
name: 'App',
components: {
Header,
Tasks,
AddTask
},
data() {
return {
tasks : [],
showAddTask : false
}
},
methods : {
deleteTask(index)
{
const that = this;
const res = fetch(`api/tasks/${index}`, {
method : "DELETE"
});
res.catch(e => alert("Error deleting task"));
res.then(function() {
/**
* this is a part where I am a bit confused
**/
that.tasks.filter(e => e.id !== index)
/**
* I use this method but it is not working
**/
that.$forceUpdate();
});
return res;
},
async fetchTask()
{
const res = await fetch("api/tasks");
const data = await res.json();
return data;
},
},
async created() {
this.tasks = await this.fetchTask();
}
}
【问题讨论】:
-
that.tasks.filter(不会改变数据。这是一个noop。没什么可刷新的,that.tasks不变,可以调试。在一般情况下,$forceUpdate是不必要的。
标签: javascript vue.js vuejs3