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)

}

})

})

相关推荐
YHHLAI7 小时前
[特殊字符] Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·人工智能
nuIl7 小时前
我把 5 个编码 Agent 塞进了一个 npm 包
前端·agent·claude
L-影7 小时前
FastAPI 静态文件:Web 页面的“固定展柜”与“加速引擎”
前端·fastapi
陪我去看海7 小时前
受够了 AI 写完代码留下一堆端口?我做了一个端口管理App!
前端·macos·vibecoding
Xuepoo7 小时前
我抛弃了 DOM,但保留了无障碍访问
前端
李明卫杭州7 小时前
Vue2 vs Vue3 的 h 函数:渲染函数完整迁移指南
前端
月光刺眼7 小时前
用LangChain 打造第一个 Agent:LLM 大脑与 Tool 手脚的完整闭环
javascript·人工智能·全栈
星栈7 小时前
LiveView 的认证系统:从登录到权限,我一开始以为有 `current_user` 就算完事了
前端·前端框架·elixir
在因斯坦7 小时前
摸摸鱼之前端自己研究 Jenkins 自动化部署
前端
西西学代码7 小时前
Flutter---底部导航栏(2)
开发语言·javascript·flutter