handleSearch() { this.getData().then( => {this.$delete(this.query_dict_cft_detail, 'current_page_from_ui')}) } 箭头前不需要数据时应该怎样写
时间: 2024-03-23 11:43:10 浏览: 84
箭头函数前不需要接收数据时,也需要写上参数的占位符,可以用下划线 `_` 来代替,例如:
```
handleSearch() {
this.getData().then(() => {
this.$delete(this.query_dict_cft_detail, 'current_page_from_ui');
// 在这里可以对获取到的数据进行处理
});
}
```
这样就可以正常编译执行了。
相关问题
handleSearch() { this.getData().then( => {this.$delete(this.query_dict_cft_detail, 'current_page_from_ui')}) } 代码补全
handleSearch() {
this.getData().then((data) => {
this.$delete(this.query_dict_cft_detail, 'current_page_from_ui');
// 在这里可以对获取到的数据进行处理
console.log(data);
});
}
export function fetchDataCft(query_dict) { return new Promise((resolve, reject) => { queryCft(query_dict).then(response => { const data = response.data const total_count = response['total_count'] // console.log(total_count) resolve(data, total_count) }).catch(error => { reject(error) }) }) } getData() { if (Object.keys(this.query_dict_mark).length === 0) { // 初始化query_dict中problem_id__in的值 this.$delete(this.query_dict, 'problem_id__in') fetchDataCft(this.query_dict).then( (data, total_count) => { this.data_list = data; this.total_count1 = total_count; }, ) } else { fetchDataMark(this.query_dict_mark).then(data => { this.problem_id_list = data.map(item => item.problem_id) this.$set(this.query_dict, 'problem_id__in', this.problem_id_list) // this.query_dict['problem_id__in'] = this.problem_id_list fetchDataCft(this.query_dict).then((data, total_count) => { this.data_list = data; this.total_count1 = total_count; }) }) } // 获取当前用户的收藏列表(problem_id_favorite) fetchDataMark({'favorite': this.current_user_name}).then(data => { this.problem_id_favorite = data.map(item => item.problem_id) }) }, total_count1的值不是预期的,请帮忙优化代码
在 `fetchDataCft` 函数中, `resolve` 方法只接受一个参数,所以需要将 `data` 和 `total_count` 封装为一个对象传递。修改后的 `fetchDataCft` 函数如下:
```
export function fetchDataCft(query_dict) {
return new Promise((resolve, reject) => {
queryCft(query_dict)
.then(response => {
const data = response.data;
const total_count = response['total_count'];
resolve({ data, total_count });
})
.catch(error => {
reject(error);
});
});
}
```
在 `getData` 函数中,当 `Object.keys(this.query_dict_mark).length === 0` 时,会调用 `fetchDataCft` 函数,但是这个函数返回的是一个 Promise,需要使用 `then` 方法来处理异步操作。修改后的代码如下:
```
getData() {
if (Object.keys(this.query_dict_mark).length === 0) {
// 初始化query_dict中problem_id__in的值
this.$delete(this.query_dict, 'problem_id__in');
fetchDataCft(this.query_dict)
.then(({ data, total_count }) => {
this.data_list = data;
this.total_count1 = total_count;
})
.catch(error => {
console.error(error);
});
} else {
fetchDataMark(this.query_dict_mark)
.then(data => {
this.problem_id_list = data.map(item => item.problem_id);
this.$set(this.query_dict, 'problem_id__in', this.problem_id_list);
// this.query_dict['problem_id__in'] = this.problem_id_list
fetchDataCft(this.query_dict)
.then(({ data, total_count }) => {
this.data_list = data;
this.total_count1 = total_count;
})
.catch(error => {
console.error(error);
});
})
.catch(error => {
console.error(error);
});
}
// 获取当前用户的收藏列表(problem_id_favorite)
fetchDataMark({ favorite: this.current_user_name })
.then(data => {
this.problem_id_favorite = data.map(item => item.problem_id);
})
.catch(error => {
console.error(error);
});
},
```
这样,无论是在 `fetchDataCft` 函数中还是在 `getData` 函数中,都能够正确地处理异步操作,避免出现不符合预期的结果。