Skip to content
泠泠彻夜的笔记
Main Navigation首页笔记
HTML
CSS
JavaScript
TypeScript
Node.js
Vue3
uniapp
插件
算法题

外观

blogs

trycatch中dialog打开失效的问题

怡然

218字小于1分钟

问题解决异步

2025-06-10

相关信息

接手其他同事的代码,在async/await中使用try/catch调用接口,接口调用成功后dialog的visible为true的操作不生效,导致dialog无法显示,经排查,监听该visible的值也监听不到任何变化,说明在try当中对visible的改变未生效。 async和await是基于Promise的语法糖,使得异步代码看起来更像同步代码。

  • 原代码
async handelQA(question) {
  this.loading = true
  this.tagLoading = true
  const sendInput = {
    question: question,
    userId: String(store.state.user.userInfo.user.userId)
  }
  try{
    const {code,data} = await handlerQAndA(sendInput)
    // xxx...
    if(code==200){
      this.QAData = this.handleData(res.data)
      this.visible=true // 不生效,监听不到变化
    }
  }catch(e){
    // xxx
  }
},
  • 修改后
handelQA(question) {
  this.loading = true
  this.tagLoading = true
  const sendInput = {
    question: question,
    userId: String(store.state.user.userInfo.user.userId)
  }
  handlerQAndA(sendInput)
    .then((res) => {
      if (res.code == 200) {
        this.QAData = this.handleData(res.data)
        this.QAVisible = true
      }
    })
    .catch((e) => {
      console.log(e)
    })
    .finally(() => {
      this.loading = false
      this.tagLoading = false
    })
},
下一页可展示自定义内容的uniapp弹窗

Power by VuePress & vuepress-theme-plume