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)

}

})

})

相关推荐
林恒smileZAZ9 分钟前
Vue<前端页面版本检测>
前端·javascript·vue.js
码事漫谈3 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
许杰小刀4 小时前
ctfshow-web文件包含(web78-web86)
android·前端·android studio
我是Superman丶4 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭4 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
xiaokuangren_5 小时前
前端css颜色
前端·css
Huanzhi_Lin5 小时前
关于V8/MajorGC/MinorGC——性能优化
javascript·性能优化·ts·js·v8·新生代·老生代
hoiii1875 小时前
C# 基于 LumiSoft 实现 SIP 客户端方案
前端·c#
anOnion5 小时前
构建无障碍组件之Meter Pattern
前端·html·交互设计
小码哥_常6 小时前
Spring Boot配置diff:解锁配置管理新姿势
前端