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)

}

})

})

相关推荐
摸鱼仙人~1 分钟前
前端面试手写核心 Cheat Sheet(终极精简版)
前端
馬致远17 分钟前
Win7 配置 Vue脚手架
javascript·vue.js·ecmascript
Ashley_Amanda20 分钟前
深入浅出Web Dynpro:SAP企业级Web应用开发全面解析
前端
方安乐23 分钟前
概念:前端工程化实践
前端
一见29 分钟前
WorkBuddy安装Skill的方法
android·java·javascript
kyriewen33 分钟前
Flexbox 完全指南:从此告别浮动,拥抱一维战神
前端·css·html
xChive33 分钟前
ECharts3D图表 | 3D柱状图和3D饼图实现思路
前端·3d·echarts
HookJames33 分钟前
解决Claude Code v2.1.74 官方找不到模型的问题-终结版
前端·chrome
代码煮茶35 分钟前
Vite 工程化实战 | 从 0 配置一个企业级前端项目(按需引入 / 环境变量 / 打包优化)
前端·vue.js
程序员Sunday35 分钟前
5000 字长文,全网最细的OpenClaw(小龙虾)架构拆解,我建议你认真看完
前端·人工智能