方法一:异步数据按顺序执行
local-api.service.ts 接口
//按模型全文检索
commonQuery(moxing,value,pageIndex,pageSize) {
const url = '/api/cms/cs/v1.0/columncontent/common/query?permission=true'
const param = {
"entity": moxing,
"filter": {
"or": [
{
"field": "title",
"value": value,
"operation": "like"
}
]
},
"sort": {
"lastmodificationtime": "desc"
},
"page": pageIndex,
"size": pageSize
}
return this.http.post(url, param,
{
headers: {
"Cookie": document.cookie
}
}
)
}
search-list.component.ts
async 将obserable数据转换.toPromise()
async getQueryData() {
this.loading = true
try {
const res1 = await this.api.commonQuery('news',this.searchContent).toPromise();
console.log('res1:', res1)
const res4 = await this.api.commonQuery('specific',this.searchContent).toPromise();
console.log('res4:', res4)
const res2 = await this.api.commonQuery('picnews',this.searchContent).toPromise();
console.log('res2:', res2)
const res3 = await this.api.commonQuery('quicklink',this.searchContent).toPromise();
console.log('res3:', res3)
this.cd.markForCheck()
//业务逻辑
this.contents = res1.data.concat(res4.data,res2.data,res3.data)
}
方法二:计时器
num为加载数据的顺序,比如四个接口同时执行,必须nun为4的时候才算执行完
let num = 0
this.api.commonQuery('news', this.searchContent).subscribe(data => {
this.newArry = this.newArry.concat(data.data)
console.log('this.newArry',this.newArry)
num++
if(num===4){
this.contents = this.newArry.sort(this.sortId)
this.loading = false
}
})
this.api.commonQuery('specific', this.searchContent).subscribe(data => {
this.newArry = this.newArry.concat(data.data)
num++
if(num===4){
this.contents = this.newArry.sort(this.sortId)
this.loading = false
}
})
this.api.commonQuery('picnews', this.searchContent).subscribe(data => {
this.newArry = this.newArry.concat(data.data)
num++
if(num===4){
this.contents = this.newArry.sort(this.sortId)
this.loading = false
}
})
this.api.commonQuery('quicklink', this.searchContent).subscribe(data => {
this.newArry = this.newArry.concat(data.data)
num++
if(num===4){
this.contents = this.newArry.sort(this.sortId)
this.loading = false
}
})