uniapp将异步api变成同步

<template>

<view class="content">

<image class="logo" src="/static/logo.png"></image>

<view class="text-area">

<text class="title">{{title}}</text>

</view>

</view>

</template>

<script>

import {

request

} from '@/tools/request/index.js'

export default {

data() {

return {

title: 'Hello',

}

},

onShow() {

this.fun1()

},

methods: {

async fun1() {

//等待执行

const rs = await this.fun2()

console.log(rs);

console.log('执行完成')

},

//耗时操作

fun2() {

return new Promise((resolve,reject) => {

uni.request({

url:'http://www.baidu.com',

success: (res) => {

resolve('ok')

},

fail: (err) => {

reject('err')

}

})

})

}

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

align-items: center;

justify-content: center;

}

.logo {

height: 200rpx;

width: 200rpx;

margin-top: 200rpx;

margin-left: auto;

margin-right: auto;

margin-bottom: 50rpx;

}

.text-area {

display: flex;

justify-content: center;

}

.title {

font-size: 36rpx;

color: #8f8f94;

}

.red {

color: red;

}

.blue {

color: blue;

}

</style>

如果是showModal

示例

return new Promise( async resolve=> {

uni.showModal({

title:'title',

content:'content',

success: (res) => {

resolve(res)

}

})

})

相关推荐
thatway19892 小时前
闲聊-关于AI终结者的警醒
前端
努力的小郑2 小时前
突发!Claude Code 51万行源码全网裸奔:一场史诗级“开源”事故,国内大厂笑麻了
前端·后端·ai编程
七度黑光3 小时前
用 openclaw 给故障复盘打分:质量审核自动化实践
运维·服务器·前端·数据库·自动化
HashTang3 小时前
Claude Code 源码中 REPL.tsx 深度解析:一个 5005 行 React 组件的架构启示
前端·后端·ai编程
wendycwb3 小时前
前端城市地址根据最后一级倒推,获取各层级id的方法
前端·vue.js·typescript
终端鹿4 小时前
Vue3 模板引用 (ref):操作 DOM 与子组件实例 从入门到精通
前端·javascript·vue.js
千寻girling4 小时前
不知道 Java 全栈 + AI 编程有没有搞头 ?
前端·人工智能·后端
小码哥_常4 小时前
Android开发:精准捕获应用的前后台行踪
前端
蜡台5 小时前
Vue 打包优化
前端·javascript·vue.js·vite·vue-cli
木斯佳5 小时前
前端八股文面经大全:快手前端一面 (2026-03-29)·面经深度解析
前端·宏任务·原型链·闭包