异步数据按顺序执行

方法一:异步数据按顺序执行

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
      }
    })
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值