笑丶日葵 2024-01-16 15:37 采纳率: 41.9%
浏览 4

vue函数返回显示问题

在vue项目中,有一个数组是遍历显示的,有些是id,有些是值,现在需要把id转为对应的值去显示,

<text style="white-space: nowrap">
                                 {{
                                    columnItem.dictCode
                                        ? getDictItems(columnItem.key, columnItem.dictCode)
                                        : dataSourceItem[columnItem.key]
                                }}
                            </text>
 getDictItems(currentId, dict) {
            console.log('getDictItems1')
            let result = null
            this.$http.get('/dict/getDictItems/' + dict).then((res) => {
                console.log('getDictItems2')
                if (res.data.success) {
                    const foundObject = res.data.result.find((obj) => obj.value == currentId)
                    const label = foundObject?.label
                    const text = foundObject?.text
                    const title = foundObject?.title
                    result = label || text || title
                } else {
                    this.$tip.error(res.data.message)
                }
            })

封装了一个这种函数去返回,但是怎么改都没有返回值.显示为空,因为请求数据没有返回函数就结束了,所以应该怎么改才能够让他等待请求结束后去返回result 值

  • 写回答

3条回答 默认 最新

  • 发狂精灵 2024-01-16 15:46
    关注

    async 配合await

    
     async getDictItems(currentId, dict) {
                console.log('getDictItems1')
                let result = null
               await this.$http.get('/dict/getDictItems/' + dict).then((res) => {
                    console.log('getDictItems2')
                    if (res.data.success) {
                        const foundObject = res.data.result.find((obj) => obj.value == currentId)
                        const label = foundObject?.label
                        const text = foundObject?.text
                        const title = foundObject?.title
                        result = label || text || title
                    } else {
                        this.$tip.error(res.data.message)
                    }
                })
    return result 
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月16日

悬赏问题

  • ¥15 Android Navigation: 某XDirections类不能自动生成
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费
  • ¥15 kafka无法正常启动(只启动了一瞬间会然后挂了)