各位大佬,如何让$emit()触发父组件的异步方法?
发布于 3 年前 作者 banyungong 3433 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

父组件方法包含一个axios请求,我用async/await让这个请求变成了同步的,但该方法变成了异步了,$emit()没等结果返回,就向下执行了,我给$emit() 加上await 无效!!!!
那么,各位大佬,如何让$emit()触发父组件的异步方法后,等待结果返回后再向下顺序执行?:sob:

子组件代码

  // 数据处理
  await this.$emit(
    'nodesHandle',
    zNodes
  )
  console.log('this is two')

父组件代码

async nodesHandle(nodeList) {
  const that = this
  const ids = nodeList.map(function (item) {
    return item['node_id']
  })
  // 查询状态(网络请求)
  const rs = await getOnlineList({
    'node_id_list': ids
  })
  const onlineList = rs.data.node_list
  // 数据处理
  ...
  console.log('this is one')
}

输出结果

this is two
this is one

期望结果

this is one
this is two

回到顶部