getDiscussList(page) { this.$http.get('discussjinrishici/list', {params: {page, limit: this.pageSize, refid: this.detail.id}}).then(res => { if (res.data.code == 0) { this.infoList = res.data.data.list; this.total = res.data.data.total; this.pageSize = res.data.data.pageSize;this.pageSizes = [this.pageSize, this.pageSize*2, this.pageSize*3, this.pageSize*5]; this.totalPage = res.data.data.totalPage; } }); }, submitForm(formName) { let sensitiveWords = ""; let sensitiveWordsArr = []; if(sensitiveWords) { sensitiveWordsArr = sensitiveWords.split(","); } for(var i=0; i<sensitiveWordsArr.length; i++){ //全局替换 var reg = new RegExp(sensitiveWordsArr[i],"g"); //判断内容中是否包括敏感词 if (this.form.content.indexOf(sensitiveWordsArr[i]) > -1) { // 将敏感词替换为 ** this.form.content = this.form.content.replace(reg,"**"); } } this.$refs[formName].validate((valid) => { if (valid) { this.form.refid = this.detail.id; this.form.avatarurl = localStorage.getItem('headportrait')?localStorage.getItem('headportrait'):''; this.$http.post('discussjinrishici/add', this.form).then(res => { if (res.data.code == 0) { this.form.content = ''; this.getDiscussList(1); this.$message({ type: 'success', message: '评论成功!', duration: 1500, }); } }); } else { return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); }, }, components: { CountDown } } </script>
时间: 2023-12-30 21:04:58 浏览: 24
这是一个Vue.js组件,包含了获取评论列表、提交评论表单、重置表单等功能。在获取评论列表时,通过发送GET请求获取指定页数的评论列表,并将数据赋值给组件的infoList、total、pageSize、totalPage等变量。在提交评论表单时,先检测评论内容中是否包含敏感词汇,如果有则将敏感词替换为**,然后通过POST请求将评论数据提交至服务器,并在返回结果为成功时,清空评论内容并重新获取评论列表。组件还包含了一个名为CountDown的子组件。
相关问题
async getDataList() { this.dataLoading = true let res = await this.$http.get('/sms/sms-is/page', { params: { ...this.queryForm, id: this.queryForm.id, areaCode: this.queryForm.areaCode, kh: this.queryForm.kh, eventType: this.queryForm.eventType, eventLevel: this.queryForm.eventLevel, depCode: this.queryForm.depCode, fieldName: this.queryForm.fieldName, groupCode: this.queryForm.groupCode, userName: this.queryForm.userName, current: this.pageIndex, size: this.pageSize } })
这段代码是一个异步方法`getDataList()`,用于获取数据列表。
首先,将`dataLoading`变量设置为`true`,表示数据正在加载中。
然后,使用`this.$http.get()`方法发起一个GET请求,请求的URL是"/sms/sms-is/page"。通过传递一个参数对象作为第二个参数,该参数对象包含了查询参数。
查询参数包括了`this.queryForm`对象中的一些属性,比如`id`、`areaCode`、`kh`等。使用`...`操作符将`this.queryForm`对象中的属性展开,并将额外的属性`current`和`size`添加到参数对象中。
接着,使用`await`关键字等待请求的结果,将结果保存在`res`变量中。
值得注意的是,这段代码使用了异步函数和`await`关键字来处理异步操作,确保在获取数据之前将`dataLoading`设置为`true`。这样可以在数据加载完成后进行相应的处理。
// 接收路由跳转传来的id getId() { this.id = this.$route.query.id }, // 获取新的页数 getNewPage(val) { this.currentPage = val }, // 分页获取帖子 getArticleByPage() { var params = { currentPage: this.currentPage, pageSize: this.pagesize, categoryId: this.$route.query.id, source: this.source, parameter: this.parameter } getArticlePage(params).then((res) => { }) },将这三段代码改写到一个async和await函数中
您可以将这三段代码重写为一个使用 async/await 的函数,如下所示:
```javascript
async fetchData() {
try {
// 接收路由跳转传来的id
this.id = this.$route.query.id;
// 获取新的页数
this.currentPage = await this.getNewPageValue();
// 分页获取帖子
var params = {
currentPage: this.currentPage,
pageSize: this.pagesize,
categoryId: this.$route.query.id,
source: this.source,
parameter: this.parameter,
};
await this.getArticlePageData(params);
} catch (error) {
console.error(error);
}
},
async getNewPageValue() {
return new Promise((resolve, reject) => {
resolve(val);
});
},
async getArticlePageData(params) {
try {
await getArticlePage(params);
} catch (error) {
console.error(error);
}
}
```
请注意,为了使这段代码完整,我假设 `getNewPage` 方法返回一个 Promise,并且 `getArticlePage` 是一个异步函数,返回一个 Promise。您需要根据实际情况进行调整和修改。