vue $emit调用父组件异步方法,返回结果后再执行子组件的方法

现象:子组件请求父组件的方法,触发数据更新,待数据返回后,才能进行后续的操作

解决:使用回调函数的方式

父组件:

 async getTaskListDetails(callback = () => {}) {
      const { pageSize, currentPage } = this.pagination
      const [res, err] = await taskListDetails({
        limit: pageSize,
        start: pageSize * (currentPage - 1)
      })
      if (err) return this.$message.error(err?.msg || '请求失败')
      if (res.code !== 200) return this.$message.error(res?.msg || '请求失败')
      this.dataList = res?.data?.list || []
      this.mainTask = res.data.main_task
      this.pagination.total = res.data.count
      callback(this.dataList)
 }

子组件:

  this.$emit('updateList', (res) => {
        this.checkTaskProgress(res)
  })

猜你喜欢

转载自blog.csdn.net/qq_33168578/article/details/124105330